CMS Development

makingmerry
Member

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)

  1. one with export_to_template_context so it does not render and I can retrieve the param
  2. 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.

 

Header partial:

<!-- announcement bar: context data -->
{% module 'announcement_bar_data'
  path='/my-theme/modules/global/header-announcement-bar',
  label='Announcement bar',
  export_to_template_context=true,
%}

<header class="
  {{ widget_data.announcement_bar_data.is_published ? "header--announcement": "" }}
  header">
  <!-- announcement bar: render -->
  <div class="header__announcement-bar">
    {% module 'header_announcement_bar'
      path='/my-theme/modules/global/header-announcement-bar',
      label='Announcement bar',
    %}
  </div>
...

module:

 

{% if module.is_published is truthy %}
  <div class="header-announcement-bar">
    <div class="
      header-announcement-bar__desc
      content-wrapper content-wrapper--l">
      {{ module.desc }}
    </div>
  </div>
{% endif %}

Fields:

[
  {
    "name": "desc",
    "label": "Description",
    "required": true,
    "locked": false,
    "type": "richtext",
    "default": "<p>One line describing this item</p>"
  },
  {
    "name": "is_published",
    "label": "Publish event",
    "sortable": false,
    "required": false,
    "locked": false,
    "type": "boolean",
    "default": true
  }
]

 

Thanks for any assistance!

2 Replies 2
adrien-gueret
Member

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"

 

0 Upvotes
makingmerry
Member

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.

0 Upvotes