# How to pass a variable from module to template?

**URL:** <https://community.hubspot.com/t/how-to-pass-a-variable-from-module-to-template/62007>\
**Category:** CMS Development\
**Created:** [July 29, 2022, 5:59pm UTC](https://community.hubspot.com/t/how-to-pass-a-variable-from-module-to-template/62007 "2022-07-29T17:59:39Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![paladinodeimari](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/paladinodeimari/32/14679_2.png) [@paladinodeimari](https://community.hubspot.com/u/paladinodeimari)\
**Post date:** [July 29, 2022, 5:59pm UTC](https://community.hubspot.com/t/how-to-pass-a-variable-from-module-to-template/62007/1 "2022-07-29T17:59:39Z")

</div>

I made this custom module:

`{% module "module_16591136360533" path="/CLC theme v3 Boilerplate/modules/cta-diventa-sponsor", label="cta-diventa-sponsor", force_vertical=True %}`

I would like to use the variable inside the module in this way:

 ![paladinodeimari_0-1659117531214.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/4/e/4ea8347b58409ac807e0bc1a64656d1253534a38.png)

I made tests but the value doesn’t pass.

module.force\_vertical is always empty.

Where am I wrong? TY

---

<div class="post-metadata">

**Author:** ![Jaycee\_Lewis](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/jaycee_lewis/32/61352_2.png) [@Jaycee\_Lewis](https://community.hubspot.com/u/Jaycee_Lewis)\
**Post date:** [July 29, 2022, 6:22pm UTC](https://community.hubspot.com/t/how-to-pass-a-variable-from-module-to-template/62007/2 "2022-07-29T18:22:46Z")

</div>

Hi, @paladinodeimari 👋 Thanks for posting! I want to make sure you’ve seen the following resource + these posts from community expert @Kevin-C:

- [**export\_to\_template\_context**](https://developers.hubspot.com/docs/cms/building-blocks/modules/export-to-template-context)
- [**Pass variables through HTML Partial Template**](https://community.hubspot.com/t5/CMS-Development/Is-that-possible-to-pass-variables-through-HTML-Partial-Template/m-p/313905#M15023)
- [**Pass variables**](https://community.hubspot.com/t5/CMS-Development/How-do-I-pass-field-values-from-module-with-a-repeater-to-a/m-p/560007#M26905)

Have fun coding! — Jaycee

---

<div class="post-metadata">

**Author:** ![paladinodeimari](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/paladinodeimari/32/14679_2.png) [@paladinodeimari](https://community.hubspot.com/u/paladinodeimari)\
**Post date:** [July 30, 2022, 2:58pm UTC](https://community.hubspot.com/t/how-to-pass-a-variable-from-module-to-template/62007/3 "2022-07-30T14:58:50Z")

</div>

Thank You for resource and posts.

On the [export\_to\_template\_context](https://developers.hubspot.com/docs/cms/building-blocks/modules/export-to-template-context) page I read that

`"export_to_template_context=Trueis not supported in custom modules, as it serves no real purpose for them"`

This is my case.

I call my custom module from a partial with

`{% module "module_16591136360533" path="/CLC theme v3 Boilerplate/modules/cta-diventa-sponsor", label="cta-diventa-sponsor", force_vertical=True %}`

Within the code of the custom module I try to access to force\_vertical parameter using

```lia-code-sample
// HubL
{{ content.widgets.name_of_module.body.parameter }}
{{ content.widgets.my_text.body.value }}

```

so in my case I try

```lia-code-sample
// HubL
{{ content.widgets.module_16591136360533.body.force_vertical}}

```

but I get no value.

---

<div class="post-metadata">

**Author:** ![BarryGrennan](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/barrygrennan/32/97794_2.png) [@BarryGrennan](https://community.hubspot.com/u/BarryGrennan)\
**Post date:** [July 30, 2022, 5:54pm UTC](https://community.hubspot.com/t/how-to-pass-a-variable-from-module-to-template/62007/4 "2022-07-30T17:54:15Z")

</div>

> Within the code of the custom module I try to access to force\_vertical parameter using

Within the module? I don’t see what you mean here. Your question was about passing a variable from module to template. If the variable is being set by the module and used within the module, you’d just include it in the module as normal, something like

`{{ module.force_vertical }}`

If you’re trying to get the value in a template you’d have something like this:

```lia-code-sample
{% module "my_module" path="/My Module", label="My Module" %}

Called value: {{ content.widgets.my_module.body.force_vertical }}

```

I’ve tested this and it’s outputting the true/false value as expected.

If what you’re talking about is all just inside the module, rather than passing it to the template and as I suspect force\_vertical is a boolean, try outputting the value of it inside the module:

`{{ module.force_vertical }}`

Then maybe try something like this for your if statement instead to test it:

`{% if module.force_vertical == "true" %} `

* * *

 ![profile2022a.jpg](https://d2qpr2jzcazej7.cloudfront.net/original/2X/7/7273a34ccfe3489fceebc8a36eca751481b59531.jpeg)

**Barry Grennan**

[Freelance HubSpot CMS Developer](https://www.seoanseo.ca?utm_source=HubSpotCommunity)

[Website](https://www.seoanseo.ca?utm_source=HubSpotCommunity) | [Contact](https://www.seoanseo.ca/#contact?utm_source=HubSpotCommunity)

---

<div class="post-metadata">

**Author:** ![paladinodeimari](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/paladinodeimari/32/14679_2.png) [@paladinodeimari](https://community.hubspot.com/u/paladinodeimari)\
**Post date:** [July 30, 2022, 8:13pm UTC](https://community.hubspot.com/t/how-to-pass-a-variable-from-module-to-template/62007/5 "2022-07-30T20:13:02Z")

</div>

Thank you Barry.

My scenario is exactly what you described:

_If the variable is being set by the module and used within the module, you’d just include it in the module as normal, something like_

`{{ module.force_vertical }}`

That was my first attempt but still it doesn’t work.

I also try this:

`{% if module.force_vertical == "true" %} `

but again it doesn’t work.

To better describe my scenario I paste the code inside the partial and the code inside the custom module:

```lia-code-sample
<!--
 templateType: global_partial
 isAvailableForNewContent: false
-->
<!-- Begin partial -->

<section id="sponsor" class="sponsor-bar">
 <div class="row-fluid-wrapper row-depth-1 row-number-1 ">
 <div class="row-fluid ">
 <div class="span12">
 <h3 class="sponsor-bar-titolo">Caffè Letterario Codroipese ringrazia</h3>
 </div>
 </div>

 {% dnd_area "dnd_loghi_sponsor" label="Loghi Sponsor" %}
 {% dnd_section %}

 {% dnd_module "module_165877787440811" path="/CLC theme v3 Boilerplate/modules/logo-sponsor", label="logo-sponsor" %} 
 {% end_dnd_module %}

 {% end_dnd_section %}
 {% end_dnd_area %}

 <section id="patrocinio" class="patrocinio-bar">
 <div class="row-fluid">
 <div class="span12">
 <p>Con il patrocinio di:</p>
 <img src="https://f.hubspotusercontent-eu1.net/hubfs/26060437/template/logo-comune-di-codroipo.jpg" alt="logo Comune di Codroipo" />
 </div>
 </div>
 </section>

 {% module "module_16591136360533" path="/CLC theme v3 Boilerplate/modules/cta-diventa-sponsor", label="cta-diventa-sponsor", force_vertical=True %}

 </div>
</section>

<!-- End partial -->

```

```lia-code-sample
{% if module.force_vertical == "true" %} 
 {% set forceVerticalClass = 'force-vertical' %}
 {% set forceVerticalSpanSx = 'span12' %}
 {% set forceVerticalSpanDx = 'span12' %}
{% else %}
 {% set forceVerticalClass = '' %}
 {% set forceVerticalSpanSx = 'span4' %}
 {% set forceVerticalSpanDx = 'span8' %}
{% endif %}
<section class="call-to-action">
 <div class="call-to-action-wrapper {{ forceVerticalClass }}">
 <div class="row-fluid">
 <div class="span4">
 <div class="call-to-action-image">
 {% if module.cta_image.src %}
 {% set sizeAttrs = 'width="{{ module.cta_image.width }}" height="{{ module.cta_image.height }}"' %}
 {% if module.cta_image.size_type == 'auto' %}
 {% set sizeAttrs = 'width="{{ module.cta_image.width }}" height="{{ module.cta_image.height }}" style="max-width: 100%; height: auto;"' %}
 {% elif module.cta_image.size_type == 'auto_custom_max' %}
 {% set sizeAttrs = 'width="{{ module.cta_image.max_width }}" height="{{ module.cta_image.max_height }}" style="max-width: 100%; height: auto;"' %}
 {% endif %}
 {% set loadingAttr = module.cta_image.loading != 'disabled' ? 'loading="{{ module.cta_image.loading }}"' : '' %}
 <img src="{{ module.cta_image.src }}" alt="{{ module.cta_image.alt }}" {{ loadingAttr }} {{ sizeAttrs }}>
 {% endif %}
 </div>
 </div>
 <div class="span8">
 <div class="call-to-action-title">
 <h4 class="{{ forceVerticalClass }}">{{ module.cta_title }}</h4>
 </div>
 <div class="call-to-action-text">
 <p class="{{ forceVerticalClass }}">{{ module.cta_text }}</p>
 </div>
 <div class="call-to-action-button">
 <a class="{{ forceVerticalClass }}" href="{{ module.cta_button_link }}" title="{{ module.cta_title }}">{{ module.cta_button_text }}</a>
 </div>
 </div>
 </div>
 </div>
</section>

```

---

<div class="post-metadata">

**Author:** ![BarryGrennan](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/barrygrennan/32/97794_2.png) [@BarryGrennan](https://community.hubspot.com/u/BarryGrennan)\
**Post date:** [July 30, 2022, 11:26pm UTC](https://community.hubspot.com/t/how-to-pass-a-variable-from-module-to-template/62007/6 "2022-07-30T23:26:56Z")

</div>

If the force\_vertical field in your module is a boolean field and inserting:

`{{ module.force_vertical }} `

inside the module doesn’t print true or false anywhere that you’re testing it, then you have to be calling it incorrectly. A boolean field called as above would either return “true” or “false”, it wouldn’t return nothing.  
Check that the variable name in the field hasn’t been changed accidently or that the field isn’t nested or something like that.

* * *

 ![profile2022a.jpg](https://d2qpr2jzcazej7.cloudfront.net/original/2X/7/7273a34ccfe3489fceebc8a36eca751481b59531.jpeg)

**Barry Grennan**

[Freelance HubSpot CMS Developer](https://www.seoanseo.ca?utm_source=HubSpotCommunity)

[Website](https://www.seoanseo.ca?utm_source=HubSpotCommunity) | [Contact](https://www.seoanseo.ca/#contact?utm_source=HubSpotCommunity)

---

<div class="post-metadata">

**Author:** ![paladinodeimari](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/paladinodeimari/32/14679_2.png) [@paladinodeimari](https://community.hubspot.com/u/paladinodeimari)\
**Post date:** [July 31, 2022, 7:19pm UTC](https://community.hubspot.com/t/how-to-pass-a-variable-from-module-to-template/62007/7 "2022-07-31T19:19:29Z")

</div>

Thank You Barry!

Inside my custom module I think the field is setup correctly:

 ![paladinodeimari_0-1659294882162.png](https://d2qpr2jzcazej7.cloudfront.net/original/2X/0/0982b9f16d0a0c8a8c62a788c7669f862570e075.png)

If I read the value of the field from within the module:

 ![paladinodeimari_1-1659294953129.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/7/8/7857bd875ef704ca23dba5b42c6cdfdb251a6ade.png)

I get “false” as output in the preview:

 ![paladinodeimari_2-1659294998905.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/d/3/d318211ba31d1fef838e234e64d3f48962673f27.png)

But if I change the content of the field from the partial, it doesn’t change:

 ![paladinodeimari_3-1659295081260.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/4/7/4713db5c3b7d15b07de0de9a8b2b1534b7ce22d7.png)

`{% module "module_16591136360533" path="/CLC theme v3 Boilerplate/modules/cta-diventa-sponsor", label="cta-diventa-sponsor", cta_force_vertical=True %}`

I set it to True but the output is always “false”.

Cannot understand why.

---

<div class="post-metadata">

**Author:** ![BarryGrennan](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/barrygrennan/32/97794_2.png) [@BarryGrennan](https://community.hubspot.com/u/BarryGrennan)\
**Post date:** [August 1, 2022, 1:06am UTC](https://community.hubspot.com/t/how-to-pass-a-variable-from-module-to-template/62007/8 "2022-08-01T01:06:48Z")

</div>

If your global partial is already on pages (which it would be if you’re testing it), editing the code won’t take effect. You’ll need to open it in the global content editor and check or uncheck the boolean field

---

<div class="post-metadata">

**Author:** ![paladinodeimari](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/paladinodeimari/32/14679_2.png) [@paladinodeimari](https://community.hubspot.com/u/paladinodeimari)\
**Post date:** [August 1, 2022, 8:41am UTC](https://community.hubspot.com/t/how-to-pass-a-variable-from-module-to-template/62007/9 "2022-08-01T08:41:37Z")

</div>

Barry, does it mean that I cannot change the value of a field in a custom module when I call it from a partial?

I mean, I thought that with this command:

`{% module "module_16591136360533" path="/CLC theme v3 Boilerplate/modules/cta-diventa-sponsor", label="cta-diventa-sponsor", cta_force_vertical=True %}`

I could change the value of “cta\_force\_vertical” field inside my custom module but, as I understand now, I cannot.

Am I right?

---

<div class="post-metadata">

**Author:** ![BarryGrennan](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/barrygrennan/32/97794_2.png) [@BarryGrennan](https://community.hubspot.com/u/BarryGrennan)\
**Post date:** [August 1, 2022, 5:29pm UTC](https://community.hubspot.com/t/how-to-pass-a-variable-from-module-to-template/62007/10 "2022-08-01T17:29:19Z")

</div>

You can change the value in a partial yes. But if it’s already in live use and has already had changes made in the global editor, your code changes are superseded by those  
One way around this would be to change the module id in the template, e.g.

`{% module "module_16591136360533_NEW" path="/CLC theme v3 Boilerplate/modules/cta-diventa-sponsor", label="cta-diventa-sponsor", cta_force_vertical=True %}`

* * *

 ![profile2022a.jpg](https://d2qpr2jzcazej7.cloudfront.net/original/2X/7/7273a34ccfe3489fceebc8a36eca751481b59531.jpeg)

**Barry Grennan**

[Freelance HubSpot CMS Developer](https://www.seoanseo.ca?utm_source=HubSpotCommunity)

[Website](https://www.seoanseo.ca?utm_source=HubSpotCommunity) | [Contact](https://www.seoanseo.ca/#contact?utm_source=HubSpotCommunity)

---

<div class="post-metadata">

**Author:** ![paladinodeimari](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/paladinodeimari/32/14679_2.png) [@paladinodeimari](https://community.hubspot.com/u/paladinodeimari)\
**Post date:** [August 1, 2022, 7:35pm UTC](https://community.hubspot.com/t/how-to-pass-a-variable-from-module-to-template/62007/11 "2022-08-01T19:35:01Z")

</div>

Thank you Barry. I try renaming but it doesnt work.

At this point I will do two custom module.

Thank you very much for support.

---

<div class="post-metadata">

**Author:** ![tony5280](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/tony5280/32/202741_2.png) [@tony5280](https://community.hubspot.com/u/tony5280)\
**Post date:** [June 5, 2024, 6:14pm UTC](https://community.hubspot.com/t/how-to-pass-a-variable-from-module-to-template/62007/12 "2024-06-05T18:14:44Z")

</div>

Two years from the dead, I wonder if this still works. I’m not getting positive results.  
In **my\_module** , I set **my\_var**.  
In my template, I dump {{ content.widgets.my\_module.body.my\_var }} and get nothing.  
Any idea why?

---

<div class="post-metadata">

**Author:** ![BarryGrennan](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/barrygrennan/32/97794_2.png) [@BarryGrennan](https://community.hubspot.com/u/BarryGrennan)\
**Post date:** [July 29, 2022, 6:42pm UTC](https://community.hubspot.com/t/how-to-pass-a-variable-from-module-to-template/62007/13 "2022-07-29T18:42:03Z")

</div>

I think Jaycee has given you the best answer. export\_to\_template\_context is super useful!  
Just wanted to add that if the module is already rendered on page you can call the data with the widgets dict e.g.

```auto
{{ content.widgets.name_of_module.body.parameter }}

{{ content.widgets.my_text.body.value }}

```

[Source](https://developers.hubspot.com/docs/cms/building-blocks/modules/export-to-template-context#retrieving-parameters-from-modules-already-rendered-on-the-template)

---

<div class="post-metadata">

**Author:** ![Jaycee\_Lewis](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/jaycee_lewis/32/61352_2.png) [@Jaycee\_Lewis](https://community.hubspot.com/u/Jaycee_Lewis)\
**Post date:** [July 29, 2022, 6:45pm UTC](https://community.hubspot.com/t/how-to-pass-a-variable-from-module-to-template/62007/14 "2022-07-29T18:45:39Z")

</div>

Thanks, @BarryGrennan 💥
