<p>To separate code and avoid duplication I am trying to split up recurring pieces of twig code into partials (think of a nav or a block of services for example).</p>

<p>I tried using <code>include</code> but Craft requires me to register a template path. This is also stated in the <a href="https://docs.craftcms.com/v2/templating-overview.html#template-paths" rel="nofollow noreferrer">docs</a>.</p>

<p>I'm not really sure this is the best way to do it since I would litter my "Sections" with "Singles" which would just function as nothing more include templates.</p>

<p>For example in my <code>home.twig</code> I have the following piece of code:</p>

<pre><code>&lt;div&gt;
    &lt;div class="container mx-auto px-4 pb-16"&gt;
        {% include 'pages/home/dienstenGrid.twig' %}
    &lt;/div&gt;
&lt;/div&gt;
</code></pre>

<p>Which would render the corresponding include template. But to be able to do this I have to register this "section"
<a href="https://i.sstatic.net/7w2mW.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/7w2mW.png" alt="enter image description here"></a></p>

<p>Something about this feels gross, and I was wondering if there's a better way to go about this issue. If <code>This is the way</code> I will use it this way (Star Wars: The Mandalorian pun intended)</p>

CC BY-SA 4.0
2019-12-02T14:48:07.257Z
Couldn't you define a macro which returns your snippet for example and then simply call `{{ macros.dienstenGrid() }}` instead?

Macros are defined in `/craft/templates/partials/macros.twig` as far as I remember
Campiotti
CC BY-SA 4.0
2019-12-02T14:49:25.950Z
That's what I was thinking, I found a thread on this stackexchange which pointed out macros. But it seems that macros are more used to create certain markup with variable inputs (like form tags) etc. But I might use macro's.
ArneDB
CC BY-SA 4.0
2019-12-02T19:30:01.497Z
Are you [extending a base layout template](https://twig.symfony.com/doc/3.x/tags/extends.html)?
Oli
CC BY-SA 4.0
2019-12-02T20:18:04.170Z
Yes I am. (I have to add more charachters to be able to comment)
ArneDB
CC BY-SA 4.0
2019-12-03T00:05:32.457Z
This shouldn't be the case, you can create any twig file without add an associate section to it. Just when you do that, it's a good idea to add ```_``` before the name. Just like ```_partials/home/dienstenGrid.twig```. Then this twig file/template is not visitable by just use the file path.
Parry Huang
CC BY-SA 4.0
2019-12-03T09:49:45.623Z
I've read your question 5 times but I still don't understand what you're expecting. Twig lets you include a template from anywhere in your templates directory. That included template is then responsible for outputting whatever you tell it to. There's no need to create singles for anything.
James Smith
CC BY-SA 4.0
2019-12-03T18:26:53.697Z
So I have done some more digging, and it's exactly like you guys say, it does work from anywhere in the template. The problem now is that I can't remember how I was doing it before. I'm trying to figure out what I was doing wrong so I could share it, but I forget. When I remember I will let you know. In any case I want to thank you for taking your time to answer my question. :S I'm sorry
ArneDB
<templating>
Is there a way to include partials in Craft without setting a path?
# Is there a way to include partials in Craft without setting a path?

(Asked by: username_0 on 2019-12-02)

To separate code and avoid duplication I am trying to split up recurring pieces of twig code into partials (think of a nav or a block of services for example).
I tried using include but Craft requires me to register a template path. This is also stated in the docs.
I'm not really sure this is the best way to do it since I would litter my "Sections" with "Singles" which would just function as nothing more include templates.
For example in my home.twig I have the following piece of code:

    
        {% include 'pages/home/dienstenGrid.twig' %}
    


Which would render the corresponding include template. But to be able to do this I have to register this "section"

Something about this feels gross, and I was wondering if there's a better way to go about this issue. If This is the way I will use it this way (Star Wars: The Mandalorian pun intended)

**Tags:** templating

> **Comments:**
> **username_1**: Couldn't you define a macro which returns your snippet for example and then simply call `{{ macros.dienstenGrid() }}` instead?  Macros are defined in `/craft/templates/partials/macros.twig` as far as I remember  
> **username_0**: That's what I was thinking, I found a thread on this stackexchange which pointed out macros. But it seems that macros are more used to create certain markup with variable inputs (like form tags) etc. But I might use macro's.  
> **username_2**: Are you [extending a base layout template](https://twig.symfony.com/doc/3.x/tags/extends.html)?  
> **username_0**: Yes I am. (I have to add more charachters to be able to comment)  
> **username_3**: This shouldn't be the case, you can create any twig file without add an associate section to it. Just when you do that, it's a good idea to add ```_``` before the name. Just like ```_partials/home/dienstenGrid.twig```. Then this twig file/template is not visitable by just use the file path.  
> **username_4**: I've read your question 5 times but I still don't understand what you're expecting. Twig lets you include a template from anywhere in your templates directory. That included template is then responsible for outputting whatever you tell it to. There's no need to create singles for anything.  
> **username_0**: So I have done some more digging, and it's exactly like you guys say, it does work from anywhere in the template. The problem now is that I can't remember how I was doing it before. I'm trying to figure out what I was doing wrong so I could share it, but I forget. When I remember I will let you know. In any case I want to thank you for taking your time to answer my question. :S I'm sorry  

---

## 0 answers

