Passing Twig Variable into Template from Include? 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 %}
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 %}