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', '<sup>HA</sup>')|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 %}
<div class="product-teaser{% if className %} // {{ className }}{% endif %}">
{% set thumbAsset = product.productImage.first() %}
{% if thumbAsset %}
<a href="{{ product.url }}">
<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"
/>
</a>
{% endif %}
<h3 class="product-teaser__title{% if className %} // {{ className }}__title{% endif %}">{{ helpers.product_title(product.title) }}</h3>
<p class="product-teaser__price{% if className %} // {{ className }}__price{% endif %}">{{ product.defaultPrice|currency(cart.currency) }}</p>
<p>
<a href="{{ product.url }}" class="btn // btn--sub">View Product</a>
{% if className == "product-teaser--featured" %}
<a href="{{ category.url }}" class="btn // btn--base">{{ category.title }} Range</a>
{% endif %}
</p>
</div>
{% endmacro %}
{% macro alsoPurchased(product, imgSize) %}
{% set cart = craft.commerce.getCart() %}
{% import '_macros/_helpers' as helpers %}
<li class="productAlsoPurchased__item">
{% set thumbAsset = product.productImage.first() %}
{% if thumbAsset %}
<a href="{{ product.url }}">
<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"
/>
</a>
{% endif %}
<p class="productAlsoPurchased__content">
<strong>{{ helpers.product_title(product.title) }}</strong><br />
<span class="product-teaser__price">{{ product.defaultPrice|currency(cart.currency) }}</span>
</p>
</li>
{% 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?