Store rendered output of module into variable

Hello,

is there an opportunity to store the rendered output of an custom module directly into an variable?

Hey @Torsten_Utz,

I could think of something, but could you describe your use-case?

best,

Anton

I wanted to nest a module into another module. I know that normaly its not possible, but maybe I can store the output of the module into an variable and then use this variable in another module.

Have you thought about using macros and calls?

Something I’ve done quite a few times:

Putting macros into macros.

An example:

You have a btn macro like

{% macro btn(link, btn_label, sr_label, style) %}
<a href="{{link}}" class="{{ style }}">
<span class="btn-label">{{ btn_label }}</span>
<span class="sr-only">{{ sr_label }}</span>
</a>
{% endmacro %}

and a card macro

{% macro card(headline, text, link, btn_label, sr_label, btn_style) %}
<div class="card">
 <h3>{{ headline }}</h3>
 <p> {{ text }}</p>
 {% if link %}
 {{ btn(link, btn_label, sr_label, btn_style)
 {% endif %}
</div>
{% endmacro %}

In your module/template simply include the macro file(s) like

{% import './path/to/single_macro_file.html' %}

^ if you like to import the whole file.

or

{% from './path/to/macro_file' import btn, card %}

^ specific macro(s) from a file

In the module/template just do it like this:

{{ card(module.headline, module.text, module.button.link, module.button.btn_label, module.button.sr_label, module.button.btn_style)}}

This let’s you create some sort of module-nesting with quite a lot of flexibility.

hope this helps

best,

Anton

Hello @Anton,
the macro solution is not good for me. But I found a soulution that I can store the module rendering into an variable. The variable can be used in a module-block inside the module_attribute tag.