I'd like to output taxonomy terms as html classes for view-rows in the views unformatted template for Drupal 8.

My code:

    {% if title %}
       <h3>{{ title }}</h3>
    {% endif %}
    {% for row in rows %}
       <div class="taxonomy-term-{{ term.id }}">
          <div{{ row.attributes }}>
             {{ row.content }}
    {% endfor %}

What am I missing?

  • I think we need more information about how your view is set up. I have a feeling it's not working because "term" may not be an available variable in this context.
    – star-szr
    Commented Jul 14, 2014 at 16:59

3 Answers 3


You're a little light on details about your setup, but I can make a suggestion based on what I see. I don't know how you're settings the term variable, but row will have access to the node (assuming this is a node view) that represents the row being displayed. You can do something similar to

{{ row.content['#node'].field_tags.getValue().0.target_id }}

(assuming field_tags is your term reference field) to print the referenced term ID.

Edit: just realized how long ago this was asked... Not sure why it was showing at the top of my list. Anyhow, hopefully helpful to someone!


You seem to be missing <div class={{ row.attributes }}>

  • actually <div{{ row.attributes }}> works just fine. <div class="taxonomy-term-{{ term.id }}"> is what is giving me trouble
    – iphipps
    Commented Jul 14, 2014 at 13:00

You need to copy existing


file from views module directory

core/modules/views/templates/views-view-unformatted.html.twig (As You already did)

Then You need to Add Field From (Taxonomy Term) View in order to get that field's value in your twig's {{ row }} variable

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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