0

I created a view with the machine name administration_utilisateur_abonnement_license

I want to add a class under the div views-row.

I created in my theme a twig file for the view views-view-fields--administration_utilisateur_abonnement_license.html.twig.

With the code below, the view is empty. What is the twig code to display the contents of the view?

{#
/**
 * @file
 * Default view template to display all the fields in a row.
 *
 * Available variables:
 * - view: The view in use.
 * - fields: A list of fields, each one contains:
 *   - content: The output of the field.
 *   - raw: The raw data for the field, if it exists. This is NOT output safe.
 *   - class: The safe class ID to use.
 *   - handler: The Views field handler controlling this field.
 *   - inline: Whether or not the field should be inline.
 *   - wrapper_element: An HTML element for a wrapper.
 *   - wrapper_attributes: List of attributes for wrapper element.
 *   - separator: An optional separator that may appear before a field.
 *   - label: The field's label text.
 *   - label_element: An HTML element for a label wrapper.
 *   - label_attributes: List of attributes for label wrapper.
 *   - label_suffix: Colon after the label.
 *   - element_type: An HTML element for the field content.
 *   - element_attributes: List of attributes for HTML element for field content.
 *   - has_label_colon: A boolean indicating whether to display a colon after
 *     the label.
 *   - element_type: An HTML element for the field content.
 *   - element_attributes: List of attributes for HTML element for field content.
 * - row: The raw result from the query, with all data it fetched.
 *
 * @see template_preprocess_views_view_fields()
 *
 * @ingroup themeable
 */
#}
<div class="block-premium">
  {{ content }}
</div>
  • For template names you normally must replace underscores with hyphens... – leymannx Aug 28 at 12:14
  • @leymannx The template works because the view is empty. But how to display all the fields ? The {{ content }} code does not work. I also tested with {{ view.content }} – mathieu Aug 28 at 12:22
0

You can take a look at core/modules/views/templates/views-view-fields.html.twig to see how the default template looks.

The field content is available under the fields variable. You can loop through the fields and print their content one by one like this:

{% for field in fields %}
  {{ field.content }}
{% endfor %}

And as @leymannx mentions, you must use dashes instead of underscores in your template name, like this views-view-fields--administration-utilisateur-abonnement-license.html.twig.

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.