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:
I made tests but the value doesn’t pass.
module.force_vertical is always empty.
Where am I wrong? TY
Hi, @paladinodeimari
Thanks for posting! I want to make sure you’ve seen the following resource + these posts from community expert @Kevin-C:
Have fun coding! — Jaycee
Thank You for resource and posts.
On the 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
// HubL
{{ content.widgets.name_of_module.body.parameter }}
{{ content.widgets.my_text.body.value }}
so in my case I try
// HubL
{{ content.widgets.module_16591136360533.body.force_vertical}}
but I get no value.
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:
{% 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" %}
Barry Grennan
Freelance HubSpot CMS Developer
Website | Contact
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:
<!--
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 -->
{% 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>
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.
Barry Grennan
Freelance HubSpot CMS Developer
Website | Contact
Thank You Barry!
Inside my custom module I think the field is setup correctly:
If I read the value of the field from within the module:
I get “false” as output in the preview:
But if I change the content of the field from the partial, it doesn’t change:
{% 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.
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
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?
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 %}
Barry Grennan
Freelance HubSpot CMS Developer
Website | Contact
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.
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?
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.
{{ content.widgets.name_of_module.body.parameter }}
{{ content.widgets.my_text.body.value }}
Source