I know similar questions have been asked before. I did my search, and indeed found similar topics here and here, among others. But I can't find a solution.

Let me explain.

I have a view which presents information from a content type called 'courses'. Information is presented in a table, using views-view-table.html.twig. I have tweaked that code to 'group' my results based on semester. All that works ok. All I want to do now is output an asterisk next to a course title and a respective text at the end of that semester. So I want a simple comparison (based on course title say) so I add an asterisk next to the title, and then another comparison so that I add some text at the end of the respective table for that semester.

Finally I did solve this problem, but I think my code is very ugly - there must be a better and more elegant way, that is why I turn here to get some help.

Say that when the course title is 'Programming Methodologies' I want to add a simple asterisk.

No matter how hard I try, I could not find a good way of doing that. Here is relevant kint output:

content - kint output

Based on this and other examples, I 've tried various stuff, but whenever I try to drill 'below' content, I always get NULL - whatever syntax I try.

I just cannot get the string 'Programming Methodologies' .. And generally I guess can't understand how to write correct syntax there. Even testing simple things like content[0] gives NULL.

Finally, thanks to this thread, and some experimentation, I managed to achieve what I want. The following code works, but seems really ugly .. I am sure there must be a better way, but I can't find. Any help is appreciated...

I post the code because I assume it will help you understand what I 'm looking for:

{% set item = content|first|first %}
              {% if item|striptags|trim|replace({"\n": "", "\r\n": "", "\t": "", "\n\r": ""}) == 'Programming Methodologies'%}
              {% endif %}

In a few words, I am looking for a better way to get the string value instead of using all those twig filters: content|first|first| ... |striptags|trim|replace etc.

In another piece of code, I tried to do something similar, again relying on multiple twig filters to drill the value I wanted ...

In views-view-table.html.twig comments its specified that a 'caption' variable is available. kint output of caption is:

caption array

Again I tried a lot but could not get the 'Semester: 01' value as a simple string, so I finally used the following piece of code which does work but seems really ugly to me. I would appreciate if someone could help with something better and cleaner, both for elegance but also for the understanding it will provide.

{% if caption|first|trim|striptags|replace({"\n": "", "\r\n": "", "\t": "", "\n\r": ""}) == 'Semester: 02' %}
  <div style="font-size: 90%; margin-top:8px;">some text here
{% endif %}
  • 2
    The linked questions are for entity templates. You can't apply them to Views templates, which are in general not very useful for Twig logic. Better try to put the Twig code in Views UI, where you can access replacement patterns for field values directly, see drupal.stackexchange.com/questions/218022/….
    – 4uk4
    Jul 24, 2019 at 14:04
  • OK thanks a lot, I had a hunch that something was wrong because I was editing a Views template, but wasn't sure. I didn't avoid bashing my head against the wall a couple hours. I will retry then as you suggest. Also, could you recommend a good source for understanding similar syntax even for entity templates - especially finding values, e.g. understanding differences between 'node' and 'content' etc. - generally understanding whats going on, most I 've managed is from trial and error and not from pure understand why. Are there any well-written, documented entity templates to look at?
    – thomas
    Jul 24, 2019 at 14:42
  • For entity templates see this great blog post from @Berdir md-systems.ch/de/blog/techblog/2017/02/20/…
    – 4uk4
    Jul 24, 2019 at 15:11
  • thanks buddy, your help has been truly valuable - both in this post but also many other posts I 've researched along the way.
    – thomas
    Jul 24, 2019 at 15:14


Your Answer

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

Browse other questions tagged or ask your own question.