Answers.Body: <p>I suggest using macro (and <code>switch</code> tag instead of <code>if</code>).</p>
  
  <pre><code>{% macro ratio(block) %}
  {% spaceless %}
      {% switch block.imageRatio %}
          {% case "thumb" %}
              imageVariantsThumb
          {% case "16by9" %}
              imageVariants16by9
          {% case "3by2" %}
              imageVariants3by2
      {% endswitch %}
  {% endspaceless %}    
  {% endmacro %}
  </code></pre>
  
  <p>Macro would be used like this:</p>
  
  <pre><code>{% import 'macro' as macroRatio %}
  {% set imageRatio = macroRatio.ratio(block)|trim %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-08T09:05:39.490Z
Answers.Text: 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?
Answers.UserDisplayName: Dan Lee
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-08T10:55:47.587Z
Answers.Text: 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?
Answers.UserDisplayName: Dan Lee
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-08T11:42:47.577Z
Answers.Text: Figured it out needed to add |trim like so: {% set ratio = m.ratio(block) %}
Answers.UserDisplayName: Dan Lee
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-07T19:19:32.063Z
Answers.LastActivityDate: 2019-01-09T10:17:24.513Z
Answers.LastEditDate: 2019-01-09T10:17:24.513Z
Answers.UserDisplayName: Piotr Pogorzelski
Answers.Body: <p>You can find the details on include for twig here: <a href="https://twig.symfony.com/doc/2.x/tags/include.html" rel="nofollow noreferrer">https://twig.symfony.com/doc/2.x/tags/include.html</a></p>
  
  <p>The short answer being</p>
  
  <pre><code>{# 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 %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-07T15:57:01.050Z
Answers.Text: I'm actually trying to pass the variables the other way round - not into the include but from it.
Answers.UserDisplayName: Dan Lee
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-07T16:01:28.660Z
Answers.Text: Sorry I misread your question.  Sounds like you might want to look into macros instead though: https://twig.symfony.com/doc/2.x/tags/macro.html
Answers.UserDisplayName: Eric Chantigny
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-07T16:29:36.930Z
Answers.Text: 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?
Answers.UserDisplayName: Dan Lee
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-01-07T15:34:51.563Z
Answers.LastActivityDate: 2019-01-07T15:34:51.563Z
Answers.UserDisplayName: Eric Chantigny
Body: <p>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.</p>
  
  <p>When I do the below get an error saying Variable "imageRatio" does not exist. What am I doing wrong?</p>
  
  <p><strong>The Main Template</strong></p>
  
  <pre><code>{# The Image Ratio include #}
  {% include '_includes/builders/images/imageVariants' %}
  
  {# Create an Image Variant #}
  {% set optimizedImages = block.image.one()[imageRatio] %}
  {% include '_includes/builders/images/imageOptimize' %}
  </code></pre>
  
  <p><strong>Image Ratio Include</strong></p>
  
  <pre><code>{# 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 %}
  </code></pre>
  
Tags: <templating>
Title: Passing Twig Variable into Template from Include?
ThreadText: # Passing Twig Variable into Template from Include?
  
  (Asked by: username_0 on 2019-01-07)
  
  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 %}
  
  ```
  
  **Tags:** templating
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2019-01-07. Score: 2 | Accepted answer
  
  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 %}
  
  ```
  
  > **Comments:**
  > **username_0**: 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?  
  > **username_0**: 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?  
  > **username_0**: Figured it out needed to add |trim like so: {% set ratio = m.ratio(block) %}  
  
  ---
  
  ### Answer by username_2 on 2019-01-07. Score: 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 %}
  
  ```
  
  > **Comments:**
  > **username_0**: I'm actually trying to pass the variables the other way round - not into the include but from it.  
  > **username_2**: Sorry I misread your question.  Sounds like you might want to look into macros instead though: https://twig.symfony.com/doc/2.x/tags/macro.html  
  > **username_0**: 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?  
  
  ---
  
  