Here we have syntax of for/foreach loop in Twig file. Please have a look the syntax..
Suppose we have array with name of 'data', so our for loop for twig will be.
{% for key, value in data %}
{{ key }} | {{ value }}
{% endfor %}
OR
In case we need key with value then our syntax will be..
<ul>
{% for key, value in data %}
{% if key!= ' '%}
<li class="{{ key }}"> {{ value }}</li>
{% endif %}
{% endfor %}
</ul>
OR
In case we just need value without key then our syntax will be..
{% for value in data %}
{{ value }}
{% endfor %}
Note: If have any suggestions or issue regarding 'Loop in twig file drupal 8' then you can ask by comments.
Suppose we have array with name of 'data', so our for loop for twig will be.
{% for key, value in data %}
{{ key }} | {{ value }}
{% endfor %}
OR
In case we need key with value then our syntax will be..
<ul>
{% for key, value in data %}
{% if key!= ' '%}
<li class="{{ key }}"> {{ value }}</li>
{% endif %}
{% endfor %}
</ul>
OR
In case we just need value without key then our syntax will be..
{% for value in data %}
{{ value }}
{% endfor %}
Note: If have any suggestions or issue regarding 'Loop in twig file drupal 8' then you can ask by comments.
Hi, Please tell how to pass views variables in custom view template ?
ReplyDeleteHi Abhishek,
ReplyDeleteFirst you have to create view according to your requirement and then create the view template file, suppose you view machine name is customer_story_product and on block_1, so your template file name will be 'views-view-fields--customer-story-product--block-1.html.twig'.
and you can render your views field variables like..
{{ fields.field_machine_name.content }} //for any other field render.
{{ fields.title.content }} //for title field
you can get these variable name by 'custom text field' "Replacement pattern" settings.
Thanks, it works for me.
ReplyDelete