Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-09-28T14:22:39.157Z
Answers.LastActivityDate: 2018-09-28T14:22:39.157Z
Answers.UserDisplayName: Michael Rosario
Answers.Body: <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>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-28T18:22:47.400Z
Answers.Text: Thank you for your response. Where should I create the macros directory, within craft/templates directory?
Answers.UserDisplayName: Shift2Design
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-28T18:24:22.970Z
Answers.Text: Yes, within your templates folder. You can name it anything you want, though.
Answers.UserDisplayName: Paul
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-28T18:44:17.397Z
Answers.Text: 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?
Answers.UserDisplayName: Shift2Design
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-28T18:45:51.433Z
Answers.Text: 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.
Answers.UserDisplayName: Shift2Design
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-28T18:46:15.420Z
Answers.Text: I always have all related macros in one file. A user macro file, an image macro file and a forms macro file for example.
Answers.UserDisplayName: Paul
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-28T18:47:28.417Z
Answers.Text: {{ navigationMacro.subnavigation(entry.url) }}
Answers.UserDisplayName: Paul
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-28T18:53:39.447Z
Answers.Text: 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?
Answers.UserDisplayName: Shift2Design
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-28T18:55:27.347Z
Answers.Text: Yes, you can. See the comment above yours ;)
Answers.UserDisplayName: Paul
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-28T18:56:31.553Z
Answers.Text: Awesome! I missed that. Got it working. Thanks for your time and help Paul!
Answers.UserDisplayName: Shift2Design
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-28T18:58:18.067Z
Answers.Text: Done and done :)
Answers.UserDisplayName: Shift2Design
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-02-28T18:01:24.530Z
Answers.LastActivityDate: 2015-02-28T18:01:24.530Z
Answers.UserDisplayName: Paul
Body: <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>
  
Tags: <templating>
Title: Craft equivalent to EE snippets for use in templates?
ThreadText: # 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](http://twig.sensiolabs.org/doc/tags/macro.html) for reusing snippets of code within templates.
  
  In your macro file (for instance, macro/userMacro.html), you can create a macro:
  
  ```
  {% macro hairColor(user) %}
      <span class="hair-{{ user.hairColor }}">{{ user.hairColor }}</span>
  {% 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 username_2!  
  > **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") }}
  
  ```
  
  
  ---
  
  