0

In the twig template file of a view with grouped rows I would like to add the title of the group as a classname. But the rendered classname from title variable is just "_" on every group. This is the generated html code of the group with title "1":

<h3 class="views-group group-_">*1*</h3>

What's wrong here? Please see my template code:

{#
/**
* @file
* Default theme implementation to display a view of unformatted rows.
*
* Available variables:
* - title: The title of this group of rows. May be empty.
* - rows: A list of the view's row items.
*   - attributes: The row's HTML attributes.
*   - content: The row's content.
* - view: The view object.
* - default_row_class: A flag indicating whether default classes should be
*   used on rows.
*
* @see template_preprocess_views_view_unformatted()
*
* @ingroup themeable
*/
#}
{% if title %}
  {%
    set classes = [
      'views-group',
      'group-' ~ title|clean_class,
    ]
  %}
  <h3{{ content_attributes.addClass(classes) }}>*{{ title }}*</h3>
{% endif %}
{% for row in rows %}
  {%
    set row_classes = [
      default_row_class ? 'views-row',
    ]
  %}
  <div{{ row.attributes.addClass(row_classes) }}>
    {{- row.content -}}
  </div>
{% endfor %}
0

Now I found it out myself: clean_class replaced all my numbers by an underscore! So in case the title consists of a number only, it is stripped to a single underscore. So an updated template file takes care of this and could look similar to this one:

      {#
  /**
   * @file
   * Default theme implementation to display a view of unformatted rows.
   *
   * Available variables:
   * - title: The title of this group of rows. May be empty.
   * - rows: A list of the view's row items.
   *   - attributes: The row's HTML attributes.
   *   - content: The row's content.
   * - view: The view object.
   * - default_row_class: A flag indicating whether default classes should be
   *   used on rows.
   *
   * @see template_preprocess_views_view_unformatted()
   *
   * @ingroup themeable
   */
  #}
  {% if title %}
    {%
        set class = 'column-' ~ title
    %}
    <div{{ content_attributes.addClass(class|clean_class) }}>
  {% endif %}

  {% for row in rows %}
    {%
      set row_classes = [
        default_row_class ? 'views-row',
      ]
    %}
    <div{{ row.attributes.addClass(row_classes) }}>
      {{- row.content -}}
    </div>
  {% endfor %}

  {% if title %}
    </div>
  {% endif %}

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.