4

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?

1 Answer 1

9

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]) %}
3
  • Belting! thank you very much for the super swift reply
    – magicspon
    Jan 21, 2015 at 12:24
  • @carlcs A variable should be in single parenthesis according to Twig docs. Mar 27, 2017 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, 2017 at 8:54

Your Answer

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

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