I am looking for a possibility to reuse some calculated values across the same page in different modules and partials.
My first try was to declare a HubL variable within a partial, and access the same variable in another partial, but without success.
{# Begin Partial 1 #}
{% set hubdb_table = hubdb_table_rows("table_name") %}
{# End Partial 1 #}
{# Begin Partial 2 #}
<table>
{# hubdb_table is not defined #}
{% for row in hubdb_table %}
<tr>
<td>
{{ row.name }}
</td>
</tr>
{% endfor %}
</table>
{# End Partial 2 #}
HubL supports global variables, which can be accessed across different modules and partials on the same page.
To declare a global variable in HubL, you can use the “set_global” function instead of the “set” function. Here’s an example:
{# Begin Partial 1 #}
{% set_global hubdb_table = hubdb_table_rows("table_name") %}
{# End Partial 1 #}
{# Begin Partial 2 #}
<table>
{% for row in hubdb_table %}
<tr>
<td>
{{ row.name }}
</td>
</tr>
{% endfor %}
</table>
{# End Partial 2 #}
“set_global” function sets the value of the “hubdb_table” variable as a global variable. This means that the variable can be accessed in any other partial or module on the same page.
By contrast, the “set” function declares a variable as a local variable that can only be accessed within the same partial or module
.
Using “set_global” function, you can declare a global variable in one partial and access it in another partial or module, as shown in the example above.
I just tried your suggestion, but this does not work at all. The editor claims an syntax error, set_global is unknown???
Futhermore, When i search the web for “hubl set_global” I do not find a single piece of information about that?
Yes, HubL does support global variables. You can declare a global variable using the {% set %} tag at the top of your template or in a separate HubL file and then include it in your other partials and modules using the {% include %} tag. I also did with the project Subway Surfers.
Here’s an example of how to declare and use a global variable in HubL:
{% set my_global_variable = “Hello, World!” %}
{% include “my-partial.html” %}
{% include “my-module.html” %}
In your “my-partial.html” file, you can access the global variable like this:
<p>{{ my_global_variable }}</p>
And in your “my-module.html” file, you can access it like this:
<div>{{ my_global_variable }}</div>
Make sure that the file where you declare the global variable is included before any files that use it.