2

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 %}
6
  • You wanna put the category name in array ? Commented Mar 17, 2015 at 13:54
  • @JCSama I want to loop the array that has the same name as category_name. Commented Mar 17, 2015 at 14:00
  • can you post, the post variable content Commented Mar 17, 2015 at 14:05
  • @JCSama this is TimberPost element, properties. Commented Mar 17, 2015 at 14:12
  • I'm not sure I get it, you have have a string category_name and you wanna loop on it !! Commented Mar 17, 2015 at 14:18

4 Answers 4

2

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!

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your solution. Unfortunately, in my environment (WordPress Timber plugin) I can't use PHP in Twig templates.
Then, I guess, you won't be able to do it the perfect way :-( You will have to manually parse every category name, like others proposed
1

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

Comments

0

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 %}

2 Comments

Thanks for your idea. I combined events and news arrays into new one (all_posts) and with {% for post in all_posts if post.has_term( feed.category_name ) %}[[post.title]]{% endfor %} I can now get posts of the category.
This workaround has helped with my issue. However, it is not exactly related to variable variable topic.
0

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 %}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.