<p>The better approach would be to pass the icon as a parameter to your macro. So I would do something like</p>

<pre><code>{% macro renderIcon(type) %}
    {% switch type %}
        {% case 'video' %}
            {% include 'video.twig' %}
        {% case 'audio' %}
            {% include 'audio.twig' %}
        {% default %}
            {% include '_generic.twig.twig' %}
    {% endswitch %}
{% endmacro %}
</code></pre>

<p>And in your template you can do </p>

<pre><code>{% import 'macro.twig' as icon %}
{{ icon.renderIcon(product.getType().handle) }}
</code></pre>

CC BY-SA 3.0
2018-03-01T15:58:00.583Z
2018-03-01T15:58:00.583Z
Robin Schambach
<p>We're using the <a href="https://github.com/pixelandtonic/DigitalProducts" rel="nofollow noreferrer">Digital Products</a> plugin from Pixel and Tonic to sell 6 different types of products. I've set up a macro with matching icons for each of the product types (image, video, etc). On our user account page, I want to show recent purchases with the icon of the product type underneath it.</p>

<p>Usually, I would write <code>{{ icons.image }}</code> or <code>{{ icons.video}}</code> to call the icon macro. But the variable needs to be dynamic if I want to display it under a dynamic list of products.</p>

<p>In order to make it dynamic, of course, I need to be able to inject different macro variables <code>image</code>, <code>video</code>, <code>etc</code> when they're called.</p>

<p>I found the <code>{{ attribute() }}</code> function, which looks like the preferred way of accessing dynamic attributes.</p>

<p>So I tried this:</p>

<p><code>{{ attribute(icons, license.product.type) }}</code></p>

<p>And get this error:</p>

<p><code>Illegal offset type in isset or empty</code></p>

<p>So I researched a bit and found that I could try <code>{{ attribute(icons, license.product.type[0]) }}</code></p>

<p>But that led to this error: <code>Property "Craft\DigitalProducts_ProductTypeModel.0" is not defined.</code></p>

<p>So, basically now I'm stuck. My lack of PHP knowledge may be holding me back here.</p>

<macro><plugin-digitalproducts>
Dynamic Macro Variable
# Dynamic Macro Variable

(Asked by: username_0 on 2018-03-01)

We're using the [Digital Products](https://github.com/pixelandtonic/DigitalProducts) plugin from Pixel and Tonic to sell 6 different types of products. I've set up a macro with matching icons for each of the product types (image, video, etc). On our user account page, I want to show recent purchases with the icon of the product type underneath it.

Usually, I would write `{{ icons.image }}` or `{{ icons.video}}` to call the icon macro. But the variable needs to be dynamic if I want to display it under a dynamic list of products.

In order to make it dynamic, of course, I need to be able to inject different macro variables `image`, `video`, `etc` when they're called.

I found the `{{ attribute() }}` function, which looks like the preferred way of accessing dynamic attributes.

So I tried this:

`{{ attribute(icons, license.product.type) }}`

And get this error:

`Illegal offset type in isset or empty`

So I researched a bit and found that I could try `{{ attribute(icons, license.product.type[0]) }}`

But that led to this error: `Property "Craft\DigitalProducts_ProductTypeModel.0" is not defined.`

So, basically now I'm stuck. My lack of PHP knowledge may be holding me back here.

**Tags:** macro, plugin-digitalproducts

---

## 1 answers

### Answer by username_1 on 2018-03-01. Score: 3 | Accepted answer

The better approach would be to pass the icon as a parameter to your macro. So I would do something like
{% macro renderIcon(type) %}
    {% switch type %}
        {% case 'video' %}
            {% include 'video.twig' %}
        {% case 'audio' %}
            {% include 'audio.twig' %}
        {% default %}
            {% include '_generic.twig.twig' %}
    {% endswitch %}
{% endmacro %}

And in your template you can do 
{% import 'macro.twig' as icon %}
{{ icon.renderIcon(product.getType().handle) }}


---

