Use a variable as a module param value

I am trying to pass this information to a custom module:

{% set parent_items = [
{
"parent_item_link": {
"url": {
"href": "https://example.com"
},
"open_in_new_tab": true,
"rel": null
},
"parent_item_label": "Example 1"
},
{
"parent_item_link": {
"url": {
"href": "https://example.net"
},
"open_in_new_tab": false,
"rel": null
},
"parent_item_label": "Example 2"
},
{
"parent_item_link": {
"url": {
"href": "https://example.org"
},
"open_in_new_tab": true,
"rel": null
},
"parent_item_label": "Example 3"
}
] %}

{% module "hero_banner"
path='../modules/breadcrumb-hero',
label='Breadcrumb Hero',
parent_items=parent_items,
current_item_label='Current Page',
offset=0
%}

This doesn’t work, but if I provide the value directly without setting it to a variable first, it works perfectly:

{% module "hero_banner"
path='../modules/breadcrumb-hero',
label='Breadcrumb Hero',
parent_items=[
 {
 "parent_item_link": {
 "url": {
 "href": "https://example.com"
 },
 "open_in_new_tab": true,
 "rel": null
 },
 "parent_item_label": "Example 1"
 },
 {
 "parent_item_link": {
 "url": {
 "href": "https://example.net"
 },
 "open_in_new_tab": false,
 "rel": null
 },
 "parent_item_label": "Example 2"
 },
 {
 "parent_item_link": {
 "url": {
 "href": "https://example.org"
 },
 "open_in_new_tab": true,
 "rel": null
 },
 "parent_item_label": "Example 3"
 }
 ],
current_item_label='Current Page',
offset=0
%}

How can I pass the value as a variable instead?

Hello @alvarofranz
use the as keyword to assign the value to a variable and then pass that variable as a parameter.

{% set parent_items = [
 {
 "parent_item_link": {
 "url": {
 "href": "https://example.com"
 },
 "open_in_new_tab": true,
 "rel": null
 },
 "parent_item_label": "Example 1"
 },
 {
 "parent_item_link": {
 "url": {
 "href": "https://example.net"
 },
 "open_in_new_tab": false,
 "rel": null
 },
 "parent_item_label": "Example 2"
 },
 {
 "parent_item_link": {
 "url": {
 "href": "https://example.org"
 },
 "open_in_new_tab": true,
 "rel": null
 },
 "parent_item_label": "Example 3"
 }
] %}

{% module "hero_banner"
 path='../modules/breadcrumb-hero',
 label='Breadcrumb Hero',
 parent_items=parent_items as parent_items_variable, # Assign the variable
 current_item_label='Current Page',
 offset=0
%}

as parent_items_variable, you assign the value of parent_items to the variable parent_items_variable. pass parent_items_variable as the parent_items parameter to the module.

Thanks, but this doesn’t work… now nothing shows at all. I’m also not really understanding what this is supposed to do.

Hi @alvarofranz,

Could you try this:

{% module "hero_banner"
path='../modules/breadcrumb-hero',
label='Breadcrumb Hero',
parent_items="{{parent_items}}",
current_item_label='Current Page',
offset=0
%}

Thanks, this works as expected. I wonder why this is the way to do it, I thought {{ }} is used to print data, not to pass it as a param.

Great question, not one I can answer tho. I tried to pass variables in different ways, and this one worked, so I went with it :sweat_smile:

Note for future readers, it seems that the quotes are not necessary.

All of this works equally fine:

{% module "whatever"
path='../modules/whatever',
give_me_some_value={{give_me_some_value}}
%}

{% module "whatever"
path='../modules/whatever',
give_me_some_value='{{give_me_some_value}}'
%}

{% module "whatever"
path='../modules/whatever',
give_me_some_value="{{give_me_some_value}}"
%}