3

I have a series of structure items, each with a product id, based on this id I would like to load in an additional template.

Something like this:

{% set embedAsset = entry.productId %}
{% include "embedAsset" %}

Any ideas?

8

Your code should work if you remove the quotation marks enclosing your variable.

{% set embedAsset = entry.productId %}
{% include embedAsset %}

If you want, you can use string concatenation to add a path or extension to it.

{% set embedAsset = '_products/' ~ entry.productId ~ '.html' %}
{% include embedAsset %}

You could also add a "fallback template", that loads in case your dynamic template does not exist (read the Twig docs for more info about this).

{% set embedAsset = '_products/' ~ entry.productId ~ '.html' %}
{% set defaultAsset = '_products/default.html' %}
{% include([embedAsset, defaultAsset]) %}
| improve this answer | |
  • Belting! thank you very much for the super swift reply – magicspon Jan 21 '15 at 12:24
  • @carlcs A variable should be in single parenthesis according to Twig docs. – Dominik Krulak Mar 27 '17 at 15:28
  • 1
    @DominikK interesting, it seems like this is new for Twig 2. See the include tag documentation for Twig 1 in comparison. – carlcs Mar 28 '17 at 8:54

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.