Skip to content
Grav 2.0 is officially stable. Read the announcement →
Archive

Evaluating twig variable in twig for loop

Started by Muut Archive 10 years ago · 5 replies · 708 views
10 years ago

Hi All!
Am I missing something really simple when trying to refer to a set twig variable in my loop here?:

"{% for post in page.find('/home').children if post.header.author_name =={{author}}}"

{{author}} was set prior to and works in other places, but doesn't appear to be evaluated in that block. Do I have any alternative ways to refer to that/other variables here? The code above works if I explicitly assign a variable there, e.g. ' =="testAuthor" '

Thanks!

10 years ago

As a correction to the above, for the last statement, I meant assigning an explicit non-twig variable value there (e.g. that string)

10 years ago

You don't need to nest twig tags:

TWIG
{% for post in page.find('/home').children if post.header.author_name == author %}
10 years ago

Thanks for the response @rhukster.

For some reason, when I use author there, I don't see anything resulted:

'{% set author = page.header.author_name %}
<h1>{{author}}</h1>
{{ page.content }}
<hr>
<h3> Pieces by {{author}}: </h3>
<div class="content-wrapper blog-content-list">
<div id="listing">

TWIG
            {% for post in page.find('/home').children if post.header.author_name == author %}
                <li class="recent-posts">
                    <strong><a href="{{ post.url }}">{{ post.title }}</a></strong>
                </li>
            {% endfor %}'

While explicitly setting the author to a string seems to work @_@. Of note, {{author}} does evaluate appropriately when used above it in other places

10 years ago

Maybe echo out {{ author }} to make sure it's set to something you expect.

10 years ago

Thanks Rhukster! Your comment made me realize my idiocy here. There was a capital character in the author_name variable which was causing the issue.

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1362 9 years ago
Archive · by Muut Archive, 9 years ago
2 940 9 years ago
Archive · by Muut Archive, 9 years ago
2 4069 9 years ago
Archive · by Muut Archive, 9 years ago
1 2959 9 years ago
Archive · by Muut Archive, 9 years ago
3 1124 9 years ago