Thursday 5 July 2018

Loop in twig file drupal 8.

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.

3 comments:

  1. Hi, Please tell how to pass views variables in custom view template ?

    ReplyDelete
  2. Hi Abhishek,

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

    ReplyDelete

How to resolve max execution time error in drupal ?

When you found error regarding 'max_execution_time' exceed, then you can follow steps for resolve this error: Steps:   You can put t...