1

Having already defined foo, I tried this:

{% for block in availability.{{ foo }} %}

This gives error:

Expected name or number

Can anyone please tell me the right way to go about this.

1
  • 1
    Can you give a better example of what you are trying to accomplish? What is foo and what is availability?
    – Victor In
    Jun 22 '14 at 15:45
5

Try this:

{% for block in availability[foo] %}

The square brackets let you access a variable variable name on an object.

As a general rule you never put {{ }} inside of {% %} tags in twig.

1

Twig isn't like ExpressionEngine, there is no "parse order" to contend with. In Twig, you cannot nest tags within other tags.

So that line of code should look more like this:

{% for block in availability.foo %}

... or even this:

{% for block in availability['foo'] %}

Beyond that, it's difficult to tell what you are intending with your code. If this doesn't solve your problem, please expand your question with more information and we can give you a better solution.

1

Many thanks for your answers and apologies for the poor example in my question. Before your answers came in I continued researching and found the twig attribute function (http://twig.sensiolabs.org/doc/functions/attribute.html). So I did this:

{% for availability in craft.entries.section('availability2014').limit(52).find( {order: 'availabilityWeekBeginning asc'} ) %}
  {% set foo = attribute (availability, ['availability', craft.request.getSegment(2)|replace('-lake','')|capitalize]|join) %}

  {% for block in foo %}
      //Loop
  {% endfor %}
{% endfor %}
1
  • Interesting, thanks for posting your solution!
    – Lindsey D
    Jun 22 '14 at 17:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.