There are some Twig arrays:
feeds
, where a feed gets category_name
;
events
, news
, announces
with posts.
Therefore, I can get posts for a category that way:
{% for feed in feeds %}
{% if feed.category_name == "events" %}
{% for post in events %}
{{post.title}}
{% endfor %}
{% endif %}
{% endfor %}
Can I get the same output (as above one loop returns) with category_name
string set as array name?
Here feed.category_name
returns events
:
{% for feed in feeds %}
{% for post in feed.category_name %} {# feed.category_name == "events" #}
{{post.title}}
{% endfor %}
{% endfor %}
I think what the question author means is – access the array using a name derived from another variable. So that extra conditions are not necessary (and most answers here do propose extra conditions).
Based on my several-minute-research, Volt alone won't let you do it. However, since you can embed PHP code in Volt templates and twig files are compiled to PHP later on anyway, you could do something like:
{% for feed in feeds %}
<?php foreach (${$feed.category_name} as $post) { ?>
{{post.title}}
<?php } ?>
{% endif %}
{% endfor %}
I have already tested this – it does work. You may want to add an extra check if the array exists, to avoid warnings:
{% for feed in feeds %}
<?php
if (!empty(${$feed.category_name})) {
foreach (${$feed.category_name} as $post) {
?>
{{post.title}}
<?php } } ?>
{% endif %}
{% endfor %}
If you don't like the idea of embedding PHP in your template – don't forget that your template is going to be compiled as PHP anyway!
The global variable _context
holds all variables in the current context, so you can do this:
{% for feed in feeds %}
{% for post in _context[feed.category_name]|default(null) %}
{{ post.title }}
{% endfor %}
{% endfor %}
The |default(null)
is required to prevent Twig from throwing an exception if the variable is not found.
See TwigFiddle
You want the conditional to be "added" to the loop? I think you mean this in Twig Documentation:
<ul>
{% for user in users if user.active %}
<li>{{ user.username|e }}</li>
{% endfor %}
</ul>
Edit
Your main issue was with the "variable variable". You can solve this with the attribute() function and combining the different feeds into one assoc array (categories?).
So perhaps something like (untested):
{% for feed in feeds %}
{% for post in attribute(categories, feed.category_name) %}
{{post.title}}
{% endfor %}
{% endfor %}
Based on your comment :
{% set new_array = news|merge(events) %}
{% for feed in feeds if attribute(feed.category_name, ['events', 'news']) %}
{% for post in new_array %}
{{post.title}}
{% endfor %}
{% endif %}
{% endfor %}