We're using the Digital Products 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.

1 Answer 1


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) }}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.