Twig Iterate Over Object Properties

Twig iterate over object properties

You can first cast the object to array. You can build own filter casting your object to array. More about filters is available here: http://twig.sensiolabs.org/doc/advanced.html#filters

It could then look like that:

{% for key, value in my_object|cast_to_array %}

How to iterate over many properties in Twig loop?

According to your snippets, I'm guessing you want something like the following:

{% for flashcard in flashcards %}
{% for word in flashcard.getWords() %}
{{ word }}<br />
{% endfor %}
{{ flashcard.getPronunciation() }}<br>
{{ flashcard.getExampleSentence() }}<br>
{% for category in flashcard.getCategories()() %}
{{ category.getName() }}<br />
{% endfor %}
{% for translation in flashcard.getTranslations() %}
{{ translation.getWord() }}<br />
{% endfor %}
{% endfor %}

Have a look at this section of the documentation. Basically if you had foo.bar, twig will test if bar is a public property of foo and if not test if there is a public getter, getBar, to fetch bar.


Some sidenotes in both of your loops, the values category, word and translation will only hold the last value of your flashcards, because you are overwriting the value each time.

Looping through objects in Twig/Symfony?

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 }}.

Twig: Looping over array of objects

You can loop over the object too, using the same key/value pair:

{%
set languages = [
{"english": "en"},
{"spanish": "es"},
{"italian": "it"},
{"german": "de"},
{"french": "fr"},
{"portuguese": "pt"},
]
%}

{% for value in languages %}
{% for k, v in value %}
Language: {{k}} - Locale: {{v}} <br />
{% endfor %}
{% endfor %}

{# output:
Language: english - Locale: en
Language: spanish - Locale: es
Language: italian - Locale: it
Language: german - Locale: de
Language: french - Locale: fr
Language: portuguese - Locale: pt
#}

Twig - Iterating over Form fields

Yes, it's a name collision related to the way Twig accesses each attribute for convenience and because FormView is declared as \ArrayAccess it has priority over object's properties. By the way, the same would happen with parent and vars properties, but let's focus on the solution now rather than on the problem.

As it is a Twig's issue the solution should be targeted on this direction. A workaround could be create a custom function that obtains the property of the FormView correctly:

public function getFunctions()
{
return array(
new TwigFunction('formview_prop', array($this, 'getFormViewProperty')),
);
}

public function getFormViewProperty(FormView $formView, string $prop)
{
// parent, children or vars
return $formView->{$prop};
}

So, when using this function you can access to the view's properties rather than form's fields (if there is a name collision):

{% for child in formview_prop(form, 'parent') %}

Then, it will iterate over all field elements (children) of the parent view. However, I'd prefer be explicit by creating three functions formview_parent, formview_children and formview_vars instead and don't pass the second parameter.


In this sense, you might need verify also whether the form has a parent view or not, so since Symfony 2.7.39, 2.8.32, 3.3.14, 3.4.1, 4.0.1 was introduced (as bugfix) a new Twig's test function named rootform that avoid this kind of collision, especially for parent property:

{% if form is rootform %}

Iterating over ArrayEntity

{% for key, files in page.extensions.downloads.all %}

works



Related Topics



Leave a reply



Submit