# Does HubL support global variables?

**URL:** <https://community.hubspot.com/t/does-hubl-support-global-variables/80641>\
**Category:** CMS Development\
**Tags:** hubdb\
**Created:** [May 5, 2023, 7:29am UTC](https://community.hubspot.com/t/does-hubl-support-global-variables/80641 "2023-05-05T07:29:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![stupkad](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/stupkad/32/63860_2.png) [@stupkad](https://community.hubspot.com/u/stupkad)\
**Post date:** [May 5, 2023, 7:29am UTC](https://community.hubspot.com/t/does-hubl-support-global-variables/80641/1 "2023-05-05T07:29:10Z")

</div>

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.

```lia-code-sample
{# 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 #}

```

Any ideas?  
Cheers,

Dietmar

---

<div class="post-metadata">

**Author:** ![himanshurauthan](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/himanshurauthan/32/53076_2.png) [@himanshurauthan](https://community.hubspot.com/u/himanshurauthan)\
**Post date:** [May 5, 2023, 9:16am UTC](https://community.hubspot.com/t/does-hubl-support-global-variables/80641/2 "2023-05-05T09:16:23Z")

</div>

Hello @stupkad

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:

```lia-code-sample
{# 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.

---

<div class="post-metadata">

**Author:** ![JCorby-Tuech](https://avatars.discourse-cdn.com/v4/letter/j/9f8e36/32.png) [@JCorby-Tuech](https://community.hubspot.com/u/JCorby-Tuech)\
**Post date:** [June 16, 2023, 11:35am UTC](https://community.hubspot.com/t/does-hubl-support-global-variables/80641/3 "2023-06-16T11:35:36Z")

</div>

`set_global` is not a valid HubL function.

---

<div class="post-metadata">

**Author:** ![stupkad](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/stupkad/32/63860_2.png) [@stupkad](https://community.hubspot.com/u/stupkad)\
**Post date:** [May 5, 2023, 10:22am UTC](https://community.hubspot.com/t/does-hubl-support-global-variables/80641/4 "2023-05-05T10:22:43Z")

</div>

Hi @himanshurauthan,

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?

Any ideas?

---

<div class="post-metadata">

**Author:** ![Anton](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/anton/32/103167_2.png) [@Anton](https://community.hubspot.com/u/Anton)\
**Post date:** [May 8, 2023, 6:09am UTC](https://community.hubspot.com/t/does-hubl-support-global-variables/80641/5 "2023-05-08T06:09:14Z")

</div>

Hi @stupkad,

you can create a dedicated variable file like “variables.html” or so and include this file in all of your templates/modules via the [import function](https://developers.hubspot.com/docs/cms/hubl/variables-macros-syntax#import)

best,

Anton

---

<div class="post-metadata">

**Author:** ![ZHyat](https://avatars.discourse-cdn.com/v4/letter/z/73ab20/32.png) [@ZHyat](https://community.hubspot.com/u/ZHyat)\
**Post date:** [May 5, 2023, 1:37pm UTC](https://community.hubspot.com/t/does-hubl-support-global-variables/80641/6 "2023-05-05T13:37:55Z")

</div>

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](https://myapkhouse.com/subway-surfers-mod-apk/).

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.
