<p>It could get unwieldy if you have too many subfolder options, BUT... You CAN supply a list of templates to Twig's <a href="http://twig.sensiolabs.org/doc/tags/include.html" rel="nofollow"><code>include</code></a> tag. Twig checks each option, and includes the first template in the list that exists:</p>

<pre><code>{% include
   [
    '_widgets/_images/_'  ~ block.type,
    '_widgets/_mockups/_' ~ block.type,
    '_widgets/_text/_'    ~ block.type,
    '_widgets/_special/_' ~ block.type
   ]
   ignore missing
%}
</code></pre>

<p><em>(The <code>ignore missing</code> tells Twig to fall back to rendering nothing if none of the templates exist. Otherwise it would throw an exception. A popular alternative is to have a default fallback template as the last item in the list.)</em></p>

<p>Bonus 2¢: I think it's good practice to keep include templates in hidden folders (i.e. <code>_widgets/</code> instead of <code>widgets/</code>) — to signify to yourself and others that the directory does not match with a public route, and that its contents are never meant to be displayed directly.</p>

CC BY-SA 3.0
2015-08-12T06:57:07.863Z
Thanks for the Bonus 2¢ - small stuff like that is handy to know.
Leah
CC BY-SA 3.0
2015-08-12T05:27:54.393Z
2015-08-12T05:27:54.393Z
Michael Rog
<p>How do I include block types that don't all sit under the one directory? My folder structure:</p>

<pre><code> - images
  - _blockName.html
 - mockups
  - ...
 - text
 - special
</code></pre>

<p>How I'm currently including the blocks (which isn't working):</p>

<pre><code>{% extends "_layout" %}

{% block content %}
    {% for block in entry.widgets %}
        {% include "widgets/_" ~ block.type ignore missing %}
    {% endfor %}
{% endblock %}
</code></pre>

<p>How do I let craft know to look under each subdirectory?</p>

<templating><matrix>
Including block templates under different subdirectories
# Including block templates under different subdirectories

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

How do I include block types that don't all sit under the one directory? My folder structure:
 - images
  - _blockName.html
 - mockups
  - ...
 - text
 - special

How I'm currently including the blocks (which isn't working):
{% extends "_layout" %}

{% block content %}
    {% for block in entry.widgets %}
        {% include "widgets/_" ~ block.type ignore missing %}
    {% endfor %}
{% endblock %}

How do I let craft know to look under each subdirectory?

**Tags:** templating, matrix


---

## 1 answers

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

It could get unwieldy if you have too many subfolder options, BUT... You CAN supply a list of templates to Twig's include tag. Twig checks each option, and includes the first template in the list that exists:
{% include
   [
    '_widgets/_images/_'  ~ block.type,
    '_widgets/_mockups/_' ~ block.type,
    '_widgets/_text/_'    ~ block.type,
    '_widgets/_special/_' ~ block.type
   ]
   ignore missing
%}

(The ignore missing tells Twig to fall back to rendering nothing if none of the templates exist. Otherwise it would throw an exception. A popular alternative is to have a default fallback template as the last item in the list.)
Bonus 2¢: I think it's good practice to keep include templates in hidden folders (i.e. _widgets/ instead of widgets/) — to signify to yourself and others that the directory does not match with a public route, and that its contents are never meant to be displayed directly.

> **Comments:**
> **username_0**: Thanks for the Bonus 2¢ - small stuff like that is handy to know.  

---

