1

I want to add a batch filter with a variable number depending on a conditional. Is this even possible this way:

{% if category.id == '647' %}
    {% set nr = '5' %}
{% else %}
    {% set nr = '4' %}
{% endif %}


{% for batch in craft.entries.section('xy')|batch('{{ nr }}') %}
    {{ batch.title }}
{% endfor %}

This doesn't work. How should I write {{ nr }} or is it impossible due to things like parse order?

1
  • Have you tried it as just batch(nr) ? without brackets and single quotes – Alec Ritson May 20 '15 at 13:20
6

Thought I would add this as an answer instead as it should solve your issue.

I think the issue with writing it like batch('{{ nr }}') is that twig sees whatever is in '' as a string, so is seeing {{ nr }} as it is.

You also don't need to echo a variable as a function parameter i.e batch({{ nr }}), you just need to reference the variable itself.

So by just writing

{% for batch in craft.entries.section('xy')|batch(nr) %}
 {{ batch.title }}
{% endfor %

Twig will take the value for nr set above and use it as the parameter :)

1
  • Of course I didn't try the easiest possibility ;) thank you. – KSPR May 20 '15 at 14:45

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.