<p>You can also do the following:</p>

<p>Create a file, for my purpose i created a file in</p>

<pre><code>_includes/somefile.html
</code></pre>

<p>Then in my templates, I reuse the code in somefile.html by using</p>

<pre><code>{{ include("_includes/somefile") }}
</code></pre>

CC BY-SA 4.0
2018-09-28T14:22:39.157Z
2018-09-28T14:22:39.157Z
Michael Rosario
<p>Craft uses Twig, and Twig uses <a href="http://twig.sensiolabs.org/doc/tags/macro.html" rel="noreferrer">macros</a> for reusing snippets of code within templates.</p>

<p>In your macro file (for instance, macro/userMacro.html), you can create a macro:</p>

<pre><code>{% macro hairColor(user) %}
    &lt;span class="hair-{{ user.hairColor }}"&gt;{{ user.hairColor }}&lt;/span&gt;
{% endmacro %}
</code></pre>

<p>Then, in any other file (or in your main _layout template, for that matter), you can import and use this macro:</p>

<pre><code>{% import "macro/userMacro" as userMacro %}

{% for user in craft.users %}
    {{ userMacro.hairColor }}
{% endfor %}
</code></pre>

<p>Place your <code>import</code>-tag at the top of your _layout file, and you will be able to use it in every template that extends it (or is included by it).</p>

CC BY-SA 3.0
2015-02-28T18:22:47.400Z
Thank you for your response. Where should I create the macros directory, within craft/templates directory?
Shift2Design
CC BY-SA 3.0
2015-02-28T18:24:22.970Z
Yes, within your templates folder. You can name it anything you want, though.
Paul
CC BY-SA 3.0
2015-02-28T18:44:17.397Z
Okay perfect. Trying to wrap my head around this. I got an example working pretty quickly. Because I won't have many macro's, does it make sense to just have a single global macros file versus one for each snippet/macro?
Shift2Design
CC BY-SA 3.0
2015-02-28T18:45:51.433Z
For example, this is a reusable subnavigation. I just need t o pass it an entry's url. I got it working passing the entire entry but couldn't figure out just sending the entry.url.
Shift2Design
CC BY-SA 3.0
2015-02-28T18:46:15.420Z
I always have all related macros in one file. A user macro file, an image macro file and a forms macro file for example.
Paul
CC BY-SA 3.0
2015-02-28T18:47:28.417Z
{{ navigationMacro.subnavigation(entry.url) }}
Paul
CC BY-SA 3.0
2015-02-28T18:53:39.447Z
Perfect! I think I understand. I have a properties macro file, import as propertyMacros in _layout and use the propertyMacros.subnav(entry). Thank you so much! My only remaining question is can I send just entry.url as a param instead of the entire entry? Or is it irrelevant?
Shift2Design
CC BY-SA 3.0
2015-02-28T18:55:27.347Z
Yes, you can. See the comment above yours ;)
Paul
CC BY-SA 3.0
2015-02-28T18:56:31.553Z
Awesome! I missed that. Got it working. Thanks for your time and help Paul!
Shift2Design
CC BY-SA 3.0
2015-02-28T18:58:18.067Z
Done and done :)
Shift2Design
CC BY-SA 3.0
2015-02-28T18:01:24.530Z
2015-02-28T18:01:24.530Z
Paul
<p>I am new to Craft from EE. I have a snippet of code which I will be reusing in several templates. I see Globals but that doesn't seem like like what I am looking for. What is the proper method of doing this within Craft? Thanks</p>

<templating>
Craft equivalent to EE snippets for use in templates?
# Craft equivalent to EE snippets for use in templates?

(Asked by: username_0 on 2015-02-28)

I am new to Craft from EE. I have a snippet of code which I will be reusing in several templates. I see Globals but that doesn't seem like like what I am looking for. What is the proper method of doing this within Craft? Thanks

**Tags:** templating

---

## 2 answers

### Answer by username_2 on 2015-02-28. Score: 6 | Accepted answer

Craft uses Twig, and Twig uses macros for reusing snippets of code within templates.
In your macro file (for instance, macro/userMacro.html), you can create a macro:
{% macro hairColor(user) %}
    {{ user.hairColor }}
{% endmacro %}

Then, in any other file (or in your main _layout template, for that matter), you can import and use this macro:
{% import "macro/userMacro" as userMacro %}

{% for user in craft.users %}
    {{ userMacro.hairColor }}
{% endfor %}

Place your import-tag at the top of your _layout file, and you will be able to use it in every template that extends it (or is included by it).

> **Comments:**
> **username_0**: Thank you for your response. Where should I create the macros directory, within craft/templates directory?  
> **username_2**: Yes, within your templates folder. You can name it anything you want, though.  
> **username_0**: Okay perfect. Trying to wrap my head around this. I got an example working pretty quickly. Because I won't have many macro's, does it make sense to just have a single global macros file versus one for each snippet/macro?  
> **username_0**: For example, this is a reusable subnavigation. I just need t o pass it an entry's url. I got it working passing the entire entry but couldn't figure out just sending the entry.url.  
> **username_2**: I always have all related macros in one file. A user macro file, an image macro file and a forms macro file for example.  
> **username_2**: {{ navigationMacro.subnavigation(entry.url) }}  
> **username_0**: Perfect! I think I understand. I have a properties macro file, import as propertyMacros in _layout and use the propertyMacros.subnav(entry). Thank you so much! My only remaining question is can I send just entry.url as a param instead of the entire entry? Or is it irrelevant?  
> **username_2**: Yes, you can. See the comment above yours ;)  
> **username_0**: Awesome! I missed that. Got it working. Thanks for your time and help Paul!  
> **username_0**: Done and done :)  

---

### Answer by username_1 on 2018-09-28. Score: 1

You can also do the following:
Create a file, for my purpose i created a file in
_includes/somefile.html

Then in my templates, I reuse the code in somefile.html by using
{{ include("_includes/somefile") }}


---

