2

i have a for loop

{% for row in rows %}
    {{ row.content }}
{% endfor %}

will print out like this

<div> one </div>
<div> two </div>
<div> three </div>
<div> ... </div>

but what i want to is access the values using a key so i can pull the values individually so i can do some thing like this

<div class="container">
    <div> one </div>
</div>

<div class="second-container">
    <div> one </div>
</div>

<div class="anyclassname">
    <div> one </div>
</div>

basically print the value anywhere i want on a div of my choice. Is it possible ?? please help

this is for a views template

  • Is this for a node template or a view template? – No Sssweat Nov 2 '16 at 6:29
  • it is for views template --- it would be great if you can suggest a good book on twig with in-depth explanations and examples... i checked out the the symphony2 site the documentation is too short.. also seems like twig does not support switch statement. – The Naga Tanker Nov 2 '16 at 6:53
1

In views-view.html.twig is not appropriate to style your row and fields.

To style them first you need override template "views_view_'format_style'" (format style specified when you edit/create a view). Here you can style row and access 'fields' variables (use kint(row) to see them).

Something like:

{% for row in rows %}
  {%
    set row_classes = [
      default_row_class ? 'views-row',
    ]
  %}
  {{ kint(row) }} {# devel #}
  <h2> {{row.content['#node'].getTitle()}} </h2> {# access fields #}
  <div{{ row.attributes.addClass(row_classes) }}>
    {{ row.content }}
  </div>
{% endfor %}

For style fields its better override field template.

For example field--node--body--article.html.twig:

{% set mode = element['#view_mode'] %}
{% if label_hidden %}
  {% for item in items %}
    {% if mode == 'full' %}
        <div{{ attributes }}>{{ item.content }}</div>
    {% elseif mode == 'teaser' %}
        <p{{ attributes }}>{{ item.content }}</p>
    {% endif %}
  {% endfor %}
{% else %}
  <div{{ attributes }}>
    <div{{ title_attributes }}>{{ label }}</div>
    {% for item in items %}
      <div{{ item.attributes }}>{{ item.content }}</div>
    {% endfor %}
  </div>
{% endif %}

source : In Drupal 8 view.html.twig file how do I access the view content such as individual field title and/or field content?

http://twig.sensiolabs.org/doc/tags/for.html

1

The fastest/easiest solution is to click on the field, then click on Re-Write Results

Use the replacement pattern for your field, and put something like this:

<div class="container">
    <div> {{ field_name }} </div>
</div>

<div class="second-container">
    <div> {{ field_name }} </div>
</div>

<div class="anyclassname">
    <div> {{ field_name }} </div>
</div>

Alternatively, you could create a views-view-fields--[view-name]--[machine-name].html.twig file and put the same code there.


If your field in question, has unlimited values:

  • Click in your field and under Multiple field settings, uncheck the Display all values in the same row box.

enter image description here


its a views page with three fields... // 2 image and a text -- all of this needs to me printed on the same div..

Then in your template file put:

<div class="container">
{{ fields.field_company_color_scheme.content }}
{{ fields.field_port_landing_feature.content }}
{{ fields.field_com.content }}
</div>
  • lets say the for loop has 7 element in it --- say "One" "two" "three" and so on... now if i were to called the field_name seven times how will the iteration work... will it cycle on its own and print the next content ? I can call the field_name had been for something like node/page for a content... – The Naga Tanker Nov 2 '16 at 8:30
  • @TheNagaTanker, There is no need to for loop with my answer. Think of it as rows, one would be row 1, two would be row 2, etc... – No Sssweat Nov 2 '16 at 9:19
  • its a views page with three fields... // 2 image and a text -- all of this needs to me printed on the same div.. – The Naga Tanker Nov 2 '16 at 9:41
  • @TheNagaTanker see updated answer. – No Sssweat Nov 2 '16 at 9:54
  • these are the fields field_company_color_scheme field_port_landing_feature field_com – The Naga Tanker Nov 2 '16 at 9:56
0

SO this is what i've come up with

so instead of using

{% for row in rows %}
    {{ row.content }}
{% endfor %}

what i did was completely remove the for loop and used this instead

<div class= red > {{ rows[0] . content}} </div>
<div class= yellow > {{ rows[1] . content}} </div>
<div class= blue > {{ rows[2] . content}} </div>
<div class= green > {{ rows[3] . content}} </div>

this way i can also control how many items are displayed and give a div name of your choice or within another div.

now the question remains on how to take apart the

  {{ rows[0] . content}}

the array within the {{ content }} part so they can be further broken apart... any Idea ???

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.