Answers.Body: <p>This is a change in how macros work in Twig 2.0, not a Craft CMS issue (though Craft 3 did signal a move to Twig 2.x).</p>
  
  <blockquote>
    <p>As of Twig 2.0, macros imported in a file are not available in child
    templates anymore (via an include call for instance). You need to
    import macros explicitly in each file where you are using them.</p>
  </blockquote>
  
  <p><a href="https://twig.symfony.com/doc/1.x/deprecated.html" rel="nofollow noreferrer">https://twig.symfony.com/doc/1.x/deprecated.html</a></p>
  
  <p>This is also documented in the Craft CMS 3 docs:</p>
  
  <p><a href="https://docs.craftcms.com/v3/changes-in-craft-3.html#twig-2" rel="nofollow noreferrer">https://docs.craftcms.com/v3/changes-in-craft-3.html#twig-2</a></p>
  
  <p>...so just import the macro again in the child template.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-06-28T16:37:11.220Z
Answers.Text: Thank you Andrew - this is first time using macros. All good now.
Answers.UserDisplayName: CreateSean
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-06-28T16:14:36.033Z
Answers.LastActivityDate: 2018-06-28T16:14:36.033Z
Answers.UserDisplayName: andrew.welch
Body: <p>re: macros I'm trying to use one for an SVG sprite. But I'm not sure what I'm doing wrong.</p>
  
  <p>macros template:</p>
  
  <pre><code>{% macro icon(iconId) %}
      &lt;svg class="icon icon-{{ iconId }}" role="img" title="{{ iconId }}"&gt;
          &lt;use xlink:href="{{ url('/assets/images/icons.svg#' ~ iconId) }}"&gt;&lt;/use&gt;
      &lt;/svg&gt;
  {% endmacro %}
  </code></pre>
  
  <p>In my layout template I have this <code>{% import '_macros' as macros %}</code> and in an another template this <code>{{ macros.icon("SIGNAL") }}</code></p>
  
  <p>Then in the site i get a twig runtime error: </p>
  
  <blockquote>
    <p>Accessing Twig_Template attributes is forbidden.</p>
  </blockquote>
  
  <p>and it highlights the line where I extend the layout template. </p>
  
  <p>Not sure why this isn't working.</p>
  
  <p>Without a macro this code works fine. but I want the macro so I don't have to type it out all the time.</p>
  
  <pre><code>&lt;svg class="icon" height="0" width="0"&gt;
   &lt;use xlink:href="/assets/images/icons.svg#SIGNAL" /&gt;
  &lt;/svg&gt;
  </code></pre>
  
Tags: <templating><craft3><macro>
Title: Craft 3 and Macros
ThreadText: # Craft 3 and Macros
  
  (Asked by: username_0 on 2018-06-28)
  
  re: macros I'm trying to use one for an SVG sprite. But I'm not sure what I'm doing wrong.
  
  macros template:
  
  ```
  {% macro icon(iconId) %}
      <svg class="icon icon-{{ iconId }}" role="img" title="{{ iconId }}">
          <use xlink:href="{{ url('/assets/images/icons.svg#' ~ iconId) }}"></use>
      </svg>
  {% endmacro %}
  
  ```
  
  In my layout template I have this `{% import '_macros' as macros %}` and in an another template this `{{ macros.icon("SIGNAL") }}`
  
  Then in the site i get a twig runtime error:
  
  > Accessing Twig\_Template attributes is forbidden.
  
  and it highlights the line where I extend the layout template.
  
  Not sure why this isn't working.
  
  Without a macro this code works fine. but I want the macro so I don't have to type it out all the time.
  
  ```
  <svg class="icon" height="0" width="0">
   <use xlink:href="/assets/images/icons.svg#SIGNAL" />
  </svg>
  
  ```
  
  **Tags:** templating, craft3, macro
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-06-28. Score: 3 | Accepted answer
  
  This is a change in how macros work in Twig 2.0, not a Craft CMS issue (though Craft 3 did signal a move to Twig 2.x).
  
  > As of Twig 2.0, macros imported in a file are not available in child
  > templates anymore (via an include call for instance). You need to
  > import macros explicitly in each file where you are using them.
  
  <https://twig.symfony.com/doc/1.x/deprecated.html>
  
  This is also documented in the Craft CMS 3 docs:
  
  <https://docs.craftcms.com/v3/changes-in-craft-3.html#twig-2>
  
  ...so just import the macro again in the child template.
  
  > **Comments:**
  > **username_0**: Thank you Andrew - this is first time using macros. All good now.  
  
  ---
  
  