<p>You aren't able to pass an import into a macro. So <code>helpers</code> needs to be imported into each macro file that uses it.</p>

<p>You can however pass the <code>cart</code> to your macros. As a side note, it's definitely best to set the <code>cart</code> variable in a global template - this means you're only ever setting it once. All templates that extend this global template then inherit <code>cart</code>. The way macro's work though means they need to have these variables <em>explicitly passed to them</em>. They don't inherit variables in the same way includes do or when you extend templates.</p>

<p>You can then pass <code>cart</code> to your macro like so:</p>

<pre><code>{# Variable set in an extended template or current template #}
{% set cart = craft.commerce.cart %}

{# Variable is passed to the macro #}
{{ teaser(product, imgSize, className, cart) }}
</code></pre>

<p>Macro file then edited to take variables:</p>

<pre><code>{% macro teaser(product, imgSize, className, cart) %}
    {# We still need to import our helpers into this macro file #}
    {% import '_macros/_helpers' as helpers %}

    {% set category = product.category.first() %}

    &lt;div class="product-teaser{% if className %} // {{ className }}{% endif %}"&gt;
        {% set thumbAsset = product.productImage.first() %}
        {% if thumbAsset %}
        &lt;a href="{{ product.url }}"&gt;
            &lt;img
                src="{{ craft.imager.transformImage(thumbAsset, { width:imgSize, height:imgSize })}}"
                srcset="{{ craft.imager.transformImage(thumbAsset, { width:imgSize, height:imgSize })}} 1x, {{ craft.imager.transformImage(thumbAsset, { width:imgSize * 2, height:imgSize * 2 }) }} 2x"
                alt="{{ product.title }}"
                class="product-teaser__img"
            /&gt;
        &lt;/a&gt;
        {% endif %}
        &lt;h3 class="product-teaser__title{% if className %} // {{ className }}__title{% endif %}"&gt;{{ helpers.product_title(product.title) }}&lt;/h3&gt;
        &lt;p class="product-teaser__price{% if className %} // {{ className }}__price{% endif %}"&gt;{{ product.defaultPrice|currency(cart.currency) }}&lt;/p&gt;

        &lt;p&gt;
            &lt;a href="{{ product.url }}" class="btn // btn--sub"&gt;View Product&lt;/a&gt;

            {% if className == "product-teaser--featured" %}
                &lt;a href="{{ category.url }}" class="btn // btn--base"&gt;{{ category.title }} Range&lt;/a&gt;
            {% endif %}
        &lt;/p&gt;
    &lt;/div&gt;
{% endmacro %}
</code></pre>

CC BY-SA 3.0
2016-09-01T18:39:10.783Z
I never knew you could pass imported macros into a macro. That opens up all sorts of possible complexity!
Marion Newlevant
CC BY-SA 3.0
2016-09-02T08:41:34.570Z
I made a mistake, you can't pass imported macros into other macros. You'll need to import them in each macro that uses it.
Luke Pearce
CC BY-SA 3.0
2016-09-01T11:56:48.927Z
2016-09-02T08:44:02.317Z
2016-09-02T08:44:02.317Z
Luke Pearce
<p>Trying to get my head around this and make my code a little DRYer.</p>

<p>I have a <code>_helpers</code> macro file. Like so:</p>

<pre><code>{% macro date_formatted(givenDate) %}
    {{ givenDate|date("l, jS F Y") }}
{% endmacro %}

{% macro product_title(product) %}
    {{ product|replace(' HA', '&lt;sup&gt;HA&lt;/sup&gt;')|raw }}
{% endmacro %}
</code></pre>

<p>This file is used in various places throughout the website and is included in various base templates using <code>{% import '_macros/_helpers' as helpers %}</code></p>

<p>I then have another macro for creating products (teasers and various code that would be repeated). This contains:</p>

<pre><code>  {% macro teaser(product, imgSize, className) %}
    {% set cart = craft.commerce.getCart() %}
    {% set category = product.category.first() %}
    {% import '_macros/_helpers' as helpers %}

    &lt;div class="product-teaser{% if className %} // {{ className }}{% endif %}"&gt;
        {% set thumbAsset = product.productImage.first() %}
        {% if thumbAsset %}
        &lt;a href="{{ product.url }}"&gt;
            &lt;img
                src="{{ craft.imager.transformImage(thumbAsset, { width:imgSize, height:imgSize })}}"
                srcset="{{ craft.imager.transformImage(thumbAsset, { width:imgSize, height:imgSize })}} 1x, {{ craft.imager.transformImage(thumbAsset, { width:imgSize * 2, height:imgSize * 2 }) }} 2x"
                alt="{{ product.title }}"
                class="product-teaser__img"
            /&gt;
        &lt;/a&gt;
        {% endif %}
        &lt;h3 class="product-teaser__title{% if className %} // {{ className }}__title{% endif %}"&gt;{{ helpers.product_title(product.title) }}&lt;/h3&gt;
        &lt;p class="product-teaser__price{% if className %} // {{ className }}__price{% endif %}"&gt;{{ product.defaultPrice|currency(cart.currency) }}&lt;/p&gt;

        &lt;p&gt;
            &lt;a href="{{ product.url }}" class="btn // btn--sub"&gt;View Product&lt;/a&gt;

            {% if className == "product-teaser--featured" %}
                &lt;a href="{{ category.url }}" class="btn // btn--base"&gt;{{ category.title }} Range&lt;/a&gt;
            {% endif %}
        &lt;/p&gt;
    &lt;/div&gt;
{% endmacro %}


{% macro alsoPurchased(product, imgSize) %}
    {% set cart = craft.commerce.getCart() %}
    {% import '_macros/_helpers' as helpers %}

    &lt;li class="productAlsoPurchased__item"&gt;
        {% set thumbAsset = product.productImage.first() %}
        {% if thumbAsset %}
        &lt;a href="{{ product.url }}"&gt;
            &lt;img
                src="{{ craft.imager.transformImage(thumbAsset, { width:imgSize, height:imgSize })}}"
                srcset="{{ craft.imager.transformImage(thumbAsset, { width:imgSize, height:imgSize })}} 1x, {{ craft.imager.transformImage(thumbAsset, { width:imgSize * 2, height:imgSize * 2 }) }} 2x"
                alt="{{ product.title }}"
                class="productAlsoPurchased__img"
            /&gt;
        &lt;/a&gt;
        {% endif %}
        &lt;p class="productAlsoPurchased__content"&gt;
            &lt;strong&gt;{{ helpers.product_title(product.title) }}&lt;/strong&gt;&lt;br /&gt;
            &lt;span class="product-teaser__price"&gt;{{ product.defaultPrice|currency(cart.currency) }}&lt;/span&gt;
        &lt;/p&gt;
    &lt;/li&gt;
{% endmacro %}
</code></pre>

<p>The problem/question I have come up against is that it seems I have to include <code>{% import '_macros/_helpers' as helpers %}</code> in each Macro and also the same goes for setting the variable for cart. <code>{% set cart = craft.commerce.getCart() %}</code></p>

<p>Is there a better way of doing this? Or a DRYer way of doing this? So I don't have to repeat my includes and setting of variables for each Macro block?</p>

<templating><plugin-craftcommerce><variable><macro>
Macros, Variables and Includes
# Macros, Variables and Includes

(Asked by: username_0 on 2016-09-01)

Trying to get my head around this and make my code a little DRYer.
I have a _helpers macro file. Like so:
{% macro date_formatted(givenDate) %}
    {{ givenDate|date("l, jS F Y") }}
{% endmacro %}

{% macro product_title(product) %}
    {{ product|replace(' HA', 'HA')|raw }}
{% endmacro %}

This file is used in various places throughout the website and is included in various base templates using {% import '_macros/_helpers' as helpers %}
I then have another macro for creating products (teasers and various code that would be repeated). This contains:
  {% macro teaser(product, imgSize, className) %}
    {% set cart = craft.commerce.getCart() %}
    {% set category = product.category.first() %}
    {% import '_macros/_helpers' as helpers %}

    
        {% set thumbAsset = product.productImage.first() %}
        {% if thumbAsset %}
        
            
        
        {% endif %}
        {{ helpers.product_title(product.title) }}
        {{ product.defaultPrice|currency(cart.currency) }}

        
            View Product

            {% if className == "product-teaser--featured" %}
                {{ category.title }} Range
            {% endif %}
        
    
{% endmacro %}


{% macro alsoPurchased(product, imgSize) %}
    {% set cart = craft.commerce.getCart() %}
    {% import '_macros/_helpers' as helpers %}

    
        {% set thumbAsset = product.productImage.first() %}
        {% if thumbAsset %}
        
            
        
        {% endif %}
        
            {{ helpers.product_title(product.title) }}
            {{ product.defaultPrice|currency(cart.currency) }}
        
    
{% endmacro %}

The problem/question I have come up against is that it seems I have to include {% import '_macros/_helpers' as helpers %} in each Macro and also the same goes for setting the variable for cart. {% set cart = craft.commerce.getCart() %}
Is there a better way of doing this? Or a DRYer way of doing this? So I don't have to repeat my includes and setting of variables for each Macro block?

**Tags:** templating, plugin-craftcommerce, variable, macro

---

## 1 answers

### Answer by username_1 on 2016-09-01. Score: 3 | Accepted answer

You aren't able to pass an import into a macro. So helpers needs to be imported into each macro file that uses it.
You can however pass the cart to your macros. As a side note, it's definitely best to set the cart variable in a global template - this means you're only ever setting it once. All templates that extend this global template then inherit cart. The way macro's work though means they need to have these variables explicitly passed to them. They don't inherit variables in the same way includes do or when you extend templates.
You can then pass cart to your macro like so:
{# Variable set in an extended template or current template #}
{% set cart = craft.commerce.cart %}

{# Variable is passed to the macro #}
{{ teaser(product, imgSize, className, cart) }}

Macro file then edited to take variables:
{% macro teaser(product, imgSize, className, cart) %}
    {# We still need to import our helpers into this macro file #}
    {% import '_macros/_helpers' as helpers %}

    {% set category = product.category.first() %}

    
        {% set thumbAsset = product.productImage.first() %}
        {% if thumbAsset %}
        
            
        
        {% endif %}
        {{ helpers.product_title(product.title) }}
        {{ product.defaultPrice|currency(cart.currency) }}

        
            View Product

            {% if className == "product-teaser--featured" %}
                {{ category.title }} Range
            {% endif %}
        
    
{% endmacro %}

> **Comments:**
> **username_2**: I never knew you could pass imported macros into a macro. That opens up all sorts of possible complexity!  
> **username_1**: I made a mistake, you can't pass imported macros into other macros. You'll need to import them in each macro that uses it.  

---

