CMS Development

BillOddie
Member

How do I access the value of a 'choice' field from different custom modules in a template?

SOLVE

I have a drag and drop template. I have a custom module which contains a choice field that is set via the content editor.

 

I want to access the value of this choice field from other custom modules in the same template.

 

I have read about 'export_to_template_context' and that it is no longer supported in custom modules. 

 

I have read the choice section of 'Hubl Supported Tags' page but am unsure how to implement that ( https://designers.hubspot.com/docs/hubl/hubl-supported-tags#choice )

 

Thanks

0 Upvotes
1 Accepted solution
lscanlan
Solution
HubSpot Alumni
HubSpot Alumni

How do I access the value of a 'choice' field from different custom modules in a template?

SOLVE

Hi BillOddie,

 

I'd suggest exporting the tag value to the template context from the head HTML, which you can do from the field labeled "Additional <head> markup". Then within each module you could access the exported value. So for example if you named your module myModule, you could add this code into the head HTML field:

 

{% choice "myModule" label='My Module', value='One', choices='One, Two', export_to_template_context=true %}

This will create a choice module "myModule", which will be editable on the page-level. Then within the modules, you could do something like this:

 

{% if widget_data.myModule.value %}
  {{ widget_data.myModule.value }}
{% endif %}

 

...which would print the value of the module.

 

Do you think this will work for what you're trying to do?

 

- Leland

Leland Scanlan

HubSpot Developer Support

View solution in original post

3 Replies 3
lscanlan
Solution
HubSpot Alumni
HubSpot Alumni

How do I access the value of a 'choice' field from different custom modules in a template?

SOLVE

Hi BillOddie,

 

I'd suggest exporting the tag value to the template context from the head HTML, which you can do from the field labeled "Additional <head> markup". Then within each module you could access the exported value. So for example if you named your module myModule, you could add this code into the head HTML field:

 

{% choice "myModule" label='My Module', value='One', choices='One, Two', export_to_template_context=true %}

This will create a choice module "myModule", which will be editable on the page-level. Then within the modules, you could do something like this:

 

{% if widget_data.myModule.value %}
  {{ widget_data.myModule.value }}
{% endif %}

 

...which would print the value of the module.

 

Do you think this will work for what you're trying to do?

 

- Leland

Leland Scanlan

HubSpot Developer Support
BillOddie
Member

How do I access the value of a 'choice' field from different custom modules in a template?

SOLVE

Hello again Leland, 

 

I have a new question which follows on from this question.

 

I was wandering of you could give any advice on this issue too...

 

https://community.hubspot.com/t5/CMS-Development/Show-dynamic-module-in-main-edit-pane-of-page-edito...

 

Many thanks

0 Upvotes
BillOddie
Member

How do I access the value of a 'choice' field from different custom modules in a template?

SOLVE

It looks like that will work absolutely perfectly, thank you very much for enlightneing me!

0 Upvotes