<p>Take 2:</p>

<p>I think you may be looking to use a <a href="http://twig.sensiolabs.org/doc/tags/macro.html" rel="nofollow">Twig Macro</a>. Here's the gist of that approach, but I don't fully understand the layout of your template yet, so my answer may be a bit out of context until I do.</p>

<p>You'll need to create your macro higher in your template or in a separate file and import it into the template you are working in. (See the docs linked above on how to import a macro for your situation.)</p>

<p>You could then simplify your block output to something like this.  I'm using fairly verbose names for this example but you can shorten the variable names to have less code if you prefer.  </p>

<pre><code>{% import "_widgets/_settings.html" as settingsMacro %}
{% set settings = block.settings %}

&lt;div style="{{ settingsMacro.backgroundColor(settings) }} {{ settingsMacro.backgroundImage(settings) }}"&gt;
  ... content here ...
&lt;/div&gt;
</code></pre>

<p>In your macro file, you'll take those settings and process them as you need to:</p>

<p><strong>_widgets/_settings.html</strong></p>

<pre><code>{% macro backgroundColor(settings) %}
  {% set backgroundColor = '' %}

  {% for row in settings %}
    background-color: {{ row.backgroundColor }};
  {% endfor %}
{% endmacro %}

{% macro backgroundImage(settings) %}
  {% set backgroundImage = '' %}

  {% for row in settings %}
    background-image: url({{ row.backgroundImage.first().url }});
  {% endfor %}
{% endmacro %}
</code></pre>

<p>I think, due to how many matrix blocks are fairly short templates, the amount of code you may need to avoid reuse still may be a couple lines after you import a macro and call the specific macros.  But like the include, this could be another way to help ensure consistency in the code being used in multiple places. Hopefully this gives you a few more ideas to work with to find a satisfying solution. I'd need to have a better understanding of your full template picture to offer a more specific solution on how to implement.</p>

<hr>

<p>Take 1: </p>

<p>If I understand correctly, I believe one way you can do what you are trying to do is by using the <code>include with</code> syntax and making sure you are passing your variables to the include template:</p>

<pre><code>{% include "_widgets/_settings" with { 
  backgroundColor: backgroundColor,
  backgroundImage: backgroundImage
} %}
</code></pre>

<p>This assumes that you are defining <code>backgroundColor</code> and <code>backgroundImage</code> before your include, and that the include has the following code: </p>

<pre><code>&lt;div style="{{ backgroundColor }} {{ backgroundImage }}"&gt;
 ... content here ...
&lt;/div&gt;
</code></pre>

<hr>

<p>Also note, adding the underscore to your folder path hides all files within it.  So, if you don't want to have to add underscores in front of every file, you still have hidden files if they are within a hidden folder, i.e: <code>_widgets/settings</code></p>

CC BY-SA 3.0
2015-08-20T03:53:38.503Z
Unfortunately that doesn't really simplify the code much. Thank you anyway.
Leah
CC BY-SA 3.0
2015-08-20T05:05:46.310Z
Aside from simplification, does it make the code work and solve the `Variable "backgroundColor" does not exist` error?  Your example is broken up over several code blocks and it's not exactly clear what the relationship is between the `block templates` and `include` you describe is.  It's likely a Twig macro or more dynamic use of includes can help, but hard to offer a solution in your context without a better understanding of the full picture in the templates.
Ben Parizek
CC BY-SA 3.0
2015-08-20T06:43:57.660Z
See updated post. Does that make it clearer?
Leah
CC BY-SA 3.0
2015-08-20T14:25:17.227Z
Hi @Anna, I've taken another shot and updated my answer to discuss Twig Macros as well.
Ben Parizek
CC BY-SA 3.0
2015-08-26T10:55:15.677Z
Take 2 was exactly what I was after. I've read up on macros to further extend your code. Thank you!
Leah
CC BY-SA 3.0
2015-08-20T02:15:38.750Z
2015-08-20T14:24:26.383Z
2015-08-20T14:24:26.383Z
Ben Parizek
<p>In each matrix block, I'm allowing users to set a background image, background color etc with a settings field.</p>

<p>In each matrix block template, I have:</p>

<pre><code>{% block content %}

  {% set backgroundColor = '' %}
  {% set backgroundImage = '' %}

  {% for row in block.settings %}
    {% set backgroundColor %}background-color: {{ row.backgroundColor }};{% endset %}
    {% set backgroundImage %}background-image: url({{ row.backgroundImage.first().url }});{% endset %}
    ... more fields go here...
  {% endfor %}

  &lt;div style="{{ backgroundColor }} {{ backgroundImage }}"&gt;
    ... content here ...
  &lt;/div&gt;

{% block content %}
</code></pre>

<p>Rather than repeating <code>{% for row in block.settings %}</code> in each template, is there a way that I could write it once and reuse it using a single include call? </p>

<p>I could use </p>

<pre><code>{% include "_widgets/_settings" with { 
  backgroundColor: backgroundColor,
  backgroundImage: backgroundImage
} %}
</code></pre>

<p>But that doesn't really reduce the amount of code duplication. Ideally I like to use a single of code to make these variables available.</p>

CC BY-SA 3.0
2015-08-20T13:55:52.823Z
Hi Anna, how are you looping through your Matrix block templates?  Do you have something like `{% include '_partials/blocks' ~ block.type %}` dynamically including each block template that you have the above code in?  Also, how does the `{% block content %}` tag fit into your template picture?  The `block` tag is typically used to override code in a parent template that is being extended, but you seem to be using it for Matrix Blocks in some way.  Can you clarify how the block templates relate to the loop of matrix blocks and how the `block` tag is being used on the page?
Ben Parizek
<templating><include>
Including and reusing common code per matrix block
# Including and reusing common code per matrix block

(Asked by: username_0 on 2015-08-20)

In each matrix block, I'm allowing users to set a background image, background color etc with a settings field.
In each matrix block template, I have:
{% block content %}

  {% set backgroundColor = '' %}
  {% set backgroundImage = '' %}

  {% for row in block.settings %}
    {% set backgroundColor %}background-color: {{ row.backgroundColor }};{% endset %}
    {% set backgroundImage %}background-image: url({{ row.backgroundImage.first().url }});{% endset %}
    ... more fields go here...
  {% endfor %}

  
    ... content here ...
  

{% block content %}

Rather than repeating {% for row in block.settings %} in each template, is there a way that I could write it once and reuse it using a single include call? 
I could use 
{% include "_widgets/_settings" with { 
  backgroundColor: backgroundColor,
  backgroundImage: backgroundImage
} %}

But that doesn't really reduce the amount of code duplication. Ideally I like to use a single of code to make these variables available.

**Tags:** templating, include
> **Comments:**
> **username_1**: Hi Anna, how are you looping through your Matrix block templates?  Do you have something like `{% include '_partials/blocks' ~ block.type %}` dynamically including each block template that you have the above code in?  Also, how does the `{% block content %}` tag fit into your template picture?  The `block` tag is typically used to override code in a parent template that is being extended, but you seem to be using it for Matrix Blocks in some way.  Can you clarify how the block templates relate to the loop of matrix blocks and how the `block` tag is being used on the page?  

---

## 1 answers

### Answer by username_1 on 2015-08-20. Score: 3 | Accepted answer

Take 2:
I think you may be looking to use a Twig Macro. Here's the gist of that approach, but I don't fully understand the layout of your template yet, so my answer may be a bit out of context until I do.
You'll need to create your macro higher in your template or in a separate file and import it into the template you are working in. (See the docs linked above on how to import a macro for your situation.)
You could then simplify your block output to something like this.  I'm using fairly verbose names for this example but you can shorten the variable names to have less code if you prefer.  
{% import "_widgets/_settings.html" as settingsMacro %}
{% set settings = block.settings %}


  ... content here ...


In your macro file, you'll take those settings and process them as you need to:
_widgets/_settings.html
{% macro backgroundColor(settings) %}
  {% set backgroundColor = '' %}

  {% for row in settings %}
    background-color: {{ row.backgroundColor }};
  {% endfor %}
{% endmacro %}

{% macro backgroundImage(settings) %}
  {% set backgroundImage = '' %}

  {% for row in settings %}
    background-image: url({{ row.backgroundImage.first().url }});
  {% endfor %}
{% endmacro %}

I think, due to how many matrix blocks are fairly short templates, the amount of code you may need to avoid reuse still may be a couple lines after you import a macro and call the specific macros.  But like the include, this could be another way to help ensure consistency in the code being used in multiple places. Hopefully this gives you a few more ideas to work with to find a satisfying solution. I'd need to have a better understanding of your full template picture to offer a more specific solution on how to implement.

Take 1: 
If I understand correctly, I believe one way you can do what you are trying to do is by using the include with syntax and making sure you are passing your variables to the include template:
{% include "_widgets/_settings" with { 
  backgroundColor: backgroundColor,
  backgroundImage: backgroundImage
} %}

This assumes that you are defining backgroundColor and backgroundImage before your include, and that the include has the following code: 

 ... content here ...



Also note, adding the underscore to your folder path hides all files within it.  So, if you don't want to have to add underscores in front of every file, you still have hidden files if they are within a hidden folder, i.e: _widgets/settings

> **Comments:**
> **username_0**: Unfortunately that doesn't really simplify the code much. Thank you anyway.  
> **username_1**: Aside from simplification, does it make the code work and solve the `Variable "backgroundColor" does not exist` error?  Your example is broken up over several code blocks and it's not exactly clear what the relationship is between the `block templates` and `include` you describe is.  It's likely a Twig macro or more dynamic use of includes can help, but hard to offer a solution in your context without a better understanding of the full picture in the templates.  
> **username_0**: See updated post. Does that make it clearer?  
> **username_1**: Hi @Anna, I've taken another shot and updated my answer to discuss Twig Macros as well.  
> **username_0**: Take 2 was exactly what I was after. I've read up on macros to further extend your code. Thank you!  

---

