Retrieving global custom module param in template partial
I'm current developing an announcement bar global module for my header partial.
It has a boolean param for if the module should show (is_published), and I would like to retrieve that param on its parent header partial for conditional styling.
I've referred to the documentation for export_to_template_context and have tried retrieving the param with {{ content.widgets.name_of_module.body.parameter }} but it returns empty:
{% module 'header_announcement_bar'
path='/my-theme/modules/global/header-announcement-bar',
label='Announcement bar',
%}
{{ content.widgets.header_announcement_bar.body.is_published is none ? "yes": "no" }} // prints yes for null
{{ content.widgets | pprint }} // prints an empty dict
However, if I set export_to_template_context=true on my custom module, I am able to retrieve the param with {{ widget_data.job_title.body.value }} :
{% module 'header_announcement_bar'
path='/my-theme/modules/global/header-announcement-bar',
label='Announcement bar',
export_to_template_context=true,
%}
{{ widget_data.header_announcement_bar.is_published is none ? "yes": "no" }} // prints no for not null
{{ widget_data.header_announcement_bar.is_published ? "published" : "not published" }} // prints published
Right now I am able to make it work for me by having duplicate global modules:(so they maintain the same param values)
one with export_to_template_context so it does not render and I can retrieve the param
the other only for rendering.
But I would prefer if there was a cleaner solution to this, because both modules get listed on the global modules listing right now.
Retrieving global custom module param in template partial
Your field is_published has a default value, it looks like my own issue where default field values seem to not be taken into account on parent template.
If you force a default value when calling your module, it seems to work better:
{% module 'header_announcement_bar'
path='/my-theme/modules/global/header-announcement-bar',
label='Announcement bar', is_published=true
%}
{{ content.widgets.header_announcement_bar.body.is_published ? "yes": "no" }} // should be "yes"
Retrieving global custom module param in template partial
Hey Adrien,
I am not sure if they are similar issues, I'm not able to retrieve any values with content.widgets from global modules (local custom modules appear to work fine, but if a page is instantiated already, you will have to update the page data to re-render).
I did give your suggestion a shot though, but it doesn't appear to work for me.
It might not be useful for my case even if it does actually, since I'll require the value of is_published from the module instead of the hard coded value.