Macros, Variables and Includes 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', '&lt;sup&gt;HA&lt;/sup&gt;')|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 %}

    &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 %}


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?
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() %}

    &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 %}