If I get an array of types using Doctrine like this:
$types = $this->getDoctrine()
->getRepository('Model:Type')
->findAll();
And then pass $types to the Twig template (as 'types') and loop through it:
{% for type in types %}
-- WHAT GOES HERE?
{% endfor %}
I've been doing a bit of reading, and I'm not even sure if this is possible? Can I only pass associative arrays through to Twig or do arrays of objects work? And if so, how can I access the public functions of the object in Twig?
Basically I want to call getName(), getUsage(), getId() and a few other public functions on the Type object.
Thanks
Then you could do it like below:
{% for type in types %}
{{ type.name }}
{{ type.usage }}
{{ type.id }}
{% endfor %}
{{ type.getName() }} also works, it's same with {{ type.name }}.
Related
I've wrote in a twig view this:
{% for catalog in box.getCatalogs() %}
{% for mod in catalog.getModel() %}
{{ dump(catalog.getModel()) }}
{% endfor %}
{% else %}
nothing
{% endfor %}
I get this in a dump
So how do get an array of collection in a twig view,?
You don't need to worry about the type of Collection object, Both ArrayCollection and PersistentCollection implement the Collection interface and work the same way. The difference is that PersistentCollection contains objects that have been persisted in the DB and might hit the database when you iterate over them.
If you use type-hinting, just hint at Doctrine\Common\Collections\Collection.
If you write
{% for mod in catalog.getModel() %}
{{ dump(mod) }}
{% endfor %}
in your twig template, you should see your objects.
I send from Symfony object which contains at-sign # in variable.
Object name is invoice and with {{ dump(invoice) }} in twig template I see object and parameter with path:
invoice[0].banSpojDod#showAs
But I dont know how to get value of this banSpojDod#showAs because there is at-sign #.
Could you help me someone please?
You could try with The attribute function can be used to access a "dynamic" attribute of a variable:
{{ attribute(invoice[0], 'banSpojDod#showAs') }}
Hope this help
Ok thanks. Problem was that I used it in loop, and some parameters not exists. I needed to add exist conditions. So my final code works:
{% for f in invoice %}
{% if attribute(f,'banSpojDod#showAs') is defined %}
{{ attribute(f,'banSpojDod#showAs') }}
{% endif %}
{% endfor %}
I have the following code :
{% set aSessionKeys = app.session.get('aBasket')|keys %}
onkeyup="calculatePrice({{ product['product_price'] }},{{ product['product_id'] }},{{ aSessionKeys }})
And I have an error :
Notice: Array to string conversion
Can you help me please? Thx in advance. So exist a solution to passe this array?
I do like this :
{% set aKeys = aSessionKeys|join(',') %}
{{ dump(aKeys) }}
It shows good, but if I passe to jquery :
calculatePrice({{ product['product_price'] }},{{ product['product_id'] }},{{ aKeys }})
When I do in js methode calculatePrice() :
function calculatePrice(price, product_id, aKeys) {
console.log(aKeys);
var x = document.getElementById("product_quantity_"+product_id);
var total_price = price * x.value;
$("#total_price_"+product_id).html(total_price+" <small> MDL</small>");
sum = 0;
$("#total_price_basket").html();
}
It shows only the first value of aKeys
You have very popular error. You are trying to display array. But how do you imagine that? You can use {{ dump(aSessionKeys) }} for that purpose or use first element: {{ aSessionKeys.0 }} (if it is not an array or object).
But if you want to print all values from this array you can run foreach loop:
{% for element in aSessionKeys %}
{# something here #}
{{ element }}
{# something here #}
{% endfor %}
Obviously (as you are using the |keys filter) aSessionKeys contains an array, and by using {{ aSessionKeys }} in the template Twig will try to output the array as a string, which doesn’t work (or at least is not what you want).
To convert the array to a string, you should use Twig’s join() filter, which will concatenate the array values using a given delimiter string. So, if – for instance – you want to join the values using a comma, this would be the code:
{{ aSessionKeys|join(',) }}
Is there any facility to generate a path for a given route and arguments, appending the query string automatically? As a temporary workaround i'm using a self made macro:
{% macro path(route, args, with_query) %}
{% spaceless %}
{% set with_query = with_query|default(false) and app.request.queryString %}
{{ path(route, args) ~ (with_query ? '?' ~ app.request.queryString : '' ) }}
{% endspaceless %}
{% endmacro %}
Is there some native function in Symfony2/Twig for doing this?
A nice thing with path Twig extension is that unknow parameters passed through the args array are automatically appended at the end of the URL as GET paramaters :
{{ path('route_id', {'routeParam':'foo', 'unknownParam':'bar'}) }}
will produce
/path/to/route/foo?unknownParam=bar
As simple as :
{{ path('route_id', app.request.query.all) }}
I have a multidimensional array where some objects exist and others don't. I keep getting a
Method "code" for object "stdClass" does not exist in...?
The code I am using in my template is:
{% for item in items %}
<p>{% if item.product.code %}{{ item.product.code }}{% endif %}</p>
{% endfor %}
Some products do not have this code and unfortunately this data structure is provided via a feed, so I cannot change it.
When I looked at the Twig documentation I interpreted that if an object or method was not there it would just return null?
Quickly did a lookup, hope this is works for you :p
defined
defined checks if a variable is defined in the current context. This is very useful if you use the strict_variables option:
{# defined works with variable names #}
{% if foo is defined %}
...
{% endif %}
{# and attributes on variables names #}
{% if foo.bar is defined %}
...
{% endif %}