I use a table format view to display a list of articles article-list
using draggable views to allow editors to order the list. I customize the content of rows depending on a boolean field is_header
and a formatted text field title_override
. I have sub-themed views-view-table--article-list.html.twig
to accomplish this. It works well enough.
In the template, I test the truth of is_header
and the non-emptiness of title_override
as follows:
{% if row.columns.field_is_header|render|striptags|trim %}
and
{% if row.columns.field_title_override|render|striptags|trim %}
These tests yield the proper results, but they make the template very slow! Anything less than render|striptags|trim
doesn't work.
QUESTION:
Is there a way to directly access the raw data values of these variables to avoid the render|striptags|trim
overhead?
I've seen suggestions to use something like row._entity.field_is_header
, but this way of using _entity
seems to work only in templates for the HTML or Unformatted view styles. Seems like there should be an easy way to do this.
As a note, switching to using an HTML or Unformatted style doesn't seem possible because AFAIK draggable views can be set up only with the table format.
ANSWER:
Thanks to 4k4 for the answer to this question (see below). To apply his/her general answer to my specific question, here is a snippet of the twig script I ended up with:
{% for key, row in rows %}
{% if view.field.field_is_header.value(view.result[key]) %}
<conditional stuff>
{% endif %}
{% if view.field.field_title_override.value(view.result[key]) %}
<stuff when the override is non-empty>
{% else %}
<stuff when the override is empty>
{% endif %}
{% endfor %}
Note that rows
is an array normally passed to the views-view-table.html.twig template, as documented in the original template's header comments. Note also that the availability of the view
array is NOT documented in the original template's header comments (not that it would have saved me without 4k4's help).
is_header
is used for other purposes in the template. At your suggestion, I did the field formatting in Rewrite Results for of one of the fields (I assume that's what you were suggesting). It works well and seems better than what I was doing. I'll try figure out a way to handleis_header
differently.