3

I'm trying to pass a variable value from an include into a template file. The variable is imageRatio. Within this include file I have the logic which sets the right image ratio. My goal with this is to have one place to set the logic which I can reuse in different templates - I'm trying to keep the code DRY.

When I do the below get an error saying Variable "imageRatio" does not exist. What am I doing wrong?

The Main Template

{# The Image Ratio include #}
{% include '_includes/builders/images/imageVariants' %}

{# Create an Image Variant #}
{% set optimizedImages = block.image.one()[imageRatio] %}
{% include '_includes/builders/images/imageOptimize' %}

Image Ratio Include

{# Define Image Varient #}
{% if block.imageRatio == 'thumb' %}
    {% set imageRatio = 'imageVariantsThumb' %}
{% elseif block.imageRatio == '16by9' %}
    {% set imageRatio = 'imageVariants16by9' %}
{% elseif block.imageRatio == '3by2' %}
    {% set imageRatio = 'imageVariants3by2' %}
{% endif %}

2 Answers 2

2

I suggest using macro (and switch tag instead of if).

{% macro ratio(block) %}
{% spaceless %}
    {% switch block.imageRatio %}
        {% case "thumb" %}
            imageVariantsThumb
        {% case "16by9" %}
            imageVariants16by9
        {% case "3by2" %}
            imageVariants3by2
    {% endswitch %}
{% endspaceless %}    
{% endmacro %}

Macro would be used like this:

{% import 'macro' as macroRatio %}
{% set imageRatio = macroRatio.ratio(block)|trim %}
3
  • Ah ok so when I reference {% set optimizedImages = block.image.one()[ratio] %} I get an error that says 'illegal offset type'. Apparently, this happens when attempting to access an array index using an object. How do I amend?
    – Dan Lee
    Commented Jan 8, 2019 at 9:05
  • Actually doing {{ dump(ratio) }} I get the following... object(Twig_Markup)#1692 (2) { ["content":"Twig_Markup":private]=> string(17) "imageVariants3by2" ["charset":"Twig_Markup":private]=> string(5) "UTF-8" } - Is there aw way to filter this?
    – Dan Lee
    Commented Jan 8, 2019 at 10:55
  • Figured it out needed to add |trim like so: {% set ratio = m.ratio(block) %}
    – Dan Lee
    Commented Jan 8, 2019 at 11:42
0

You can find the details on include for twig here: https://twig.symfony.com/doc/2.x/tags/include.html

The short answer being

{# template.html will have access to the variables from the current context and the additional ones provided #}
{% include 'template.html' with {'foo': 'bar'} %}

{% set vars = {'foo': 'bar'} %}
{% include 'template.html' with vars %}
3
  • I'm actually trying to pass the variables the other way round - not into the include but from it.
    – Dan Lee
    Commented Jan 7, 2019 at 15:57
  • Sorry I misread your question. Sounds like you might want to look into macros instead though: twig.symfony.com/doc/2.x/tags/macro.html Commented Jan 7, 2019 at 16:01
  • Right - I'm trying to find some documentation examples of macros that use field values but can't seem to find anything. Are you sure this would be a macro?
    – Dan Lee
    Commented Jan 7, 2019 at 16:29

Your Answer

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

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