# Variables inside JavaScript

**URL:** <https://discourse.gohugo.io/t/variables-inside-javascript/39086>\
**Category:** support\
**Created:** [June 14, 2022, 12:58am UTC](https://discourse.gohugo.io/t/variables-inside-javascript/39086 "2022-06-14T00:58:10Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![tut](https://avatars.discourse-cdn.com/v4/letter/t/e95f7d/32.png) [@tut](https://discourse.gohugo.io/u/tut)\
**Post date:** [June 14, 2022, 12:58am UTC](https://discourse.gohugo.io/t/variables-inside-javascript/39086/1 "2022-06-14T00:58:10Z")

</div>

Is this still the case today?

> [@Inserting "Templates Variables" in Javascript](https://discourse.gohugo.io/t/inserting-templates-variables-in-javascript/4246/3):
>
> If you want to add variables in Javacsript you will have to inline the Javascript in the template file.

---

<div class="post-metadata">

**Author:** ![pamubay](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/pamubay/32/17838_2.png) [@pamubay](https://discourse.gohugo.io/u/pamubay)\
**Post date:** [June 14, 2022, 1:58am UTC](https://discourse.gohugo.io/t/variables-inside-javascript/39086/2 "2022-06-14T01:58:52Z")

</div>

No, you can use `@params` to pass any variables to your javascript processed by `js.Build`.

> [@Access template variables in JS](https://discourse.gohugo.io/t/access-template-variables-in-js/39030/4):
>
> There is if you’re using Hugo Pipes to compile your script. You can add params to it which can later be invoked in the script. basically you can call your script this way: {{ $js := resources.Get "/js/indexjs" | js.Build (dict "params" (dict "baseurl" site.BaseURL")) }} and then in your script: import \* as params from '@params'; console.log(params.baseurl) More [here](https://gohugo.io/hugo-pipes/js#options)

---

<div class="post-metadata">

**Author:** ![tut](https://avatars.discourse-cdn.com/v4/letter/t/e95f7d/32.png) [@tut](https://discourse.gohugo.io/u/tut)\
**Post date:** [June 14, 2022, 2:58am UTC](https://discourse.gohugo.io/t/variables-inside-javascript/39086/3 "2022-06-14T02:58:13Z")

</div>

How would it work with such a code? I want `{{ . }}` to be picked up by the JS.

```auto
{{- with site.Params.google.analytics }}
<script >
window.minimalAnalytics = {
  trackingId: '{{ . }}',
  autoTrack: true, // <-- init tracking
};
</script>
{{- end }}

```

Edit: [This](https://discourse.gohugo.io/t/access-template-variables-in-js/39030/3) worked. But I am still interested to know if/how the `@params` method would work.

---

<div class="post-metadata">

**Author:** ![pamubay](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/pamubay/32/17838_2.png) [@pamubay](https://discourse.gohugo.io/u/pamubay)\
**Post date:** [June 14, 2022, 4:07am UTC](https://discourse.gohugo.io/t/variables-inside-javascript/39086/4 "2022-06-14T04:07:21Z")

</div>

Something like this:

**assets/script.js** :

```nohighlight
import params from "@params";

window.minimalAnalytics = {
  trackingId: params.trackingId,
  autoTrack: true, // <-- init tracking
};

```

* * *

**In HTML template:**

```nohighlight
{{- $js := resources.Get "script.js" -}}
{{- $opts := dict 
  "params" (dict "trackingId" site.Params.google.analytics) 
-}}
{{- $js = $js | js.Build $opts -}}
<script src="{{ $js.Permalink }}"></script>

```

**EDIT:** Fixed a typo

---

<div class="post-metadata">

**Author:** ![tut](https://avatars.discourse-cdn.com/v4/letter/t/e95f7d/32.png) [@tut](https://discourse.gohugo.io/u/tut)\
**Post date:** [June 14, 2022, 4:51am UTC](https://discourse.gohugo.io/t/variables-inside-javascript/39086/5 "2022-06-14T04:51:49Z")

</div>

Getting error `parse failed unterminated quoted string in action`. Also, tried `site.Params.google.analytics` with and without quotes, but the javascript is printed “as is”.

```auto
(() => {
  // <stdin>
  window.minimalAnalytics = {
    trackingId: "params.trackingId",
    autoTrack: true
  };
})();

```

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [June 14, 2022, 7:42am UTC](https://discourse.gohugo.io/t/variables-inside-javascript/39086/6 "2022-06-14T07:42:19Z")

</div>

> [@pamubay](#):
>
> ```auto
> {{- $js := resources.Get "script.js" -}}
> {{- $opts := dict 
> "params" (dict "trackingId" "site.Params.google.analytics) 
> -}}
> {{- $js = $js | js.Build $opts -}}
> <script src="{{ $js.Permalink }}"></script>
> 
> ```

So @pamubay is, as always, right. Passing `params` as an option to `js.Build` is the very best/simplest way to pass configuration etc. to the JS side. But I think there is a typo with the quotes in the example with the quoute before `site`.

---

<div class="post-metadata">

**Author:** ![pamubay](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/pamubay/32/17838_2.png) [@pamubay](https://discourse.gohugo.io/u/pamubay)\
**Post date:** [June 14, 2022, 8:40am UTC](https://discourse.gohugo.io/t/variables-inside-javascript/39086/7 "2022-06-14T08:40:04Z")

</div>

Sorry, fixed.

---

<div class="post-metadata">

**Author:** ![tut](https://avatars.discourse-cdn.com/v4/letter/t/e95f7d/32.png) [@tut](https://discourse.gohugo.io/u/tut)\
**Post date:** [June 14, 2022, 12:07pm UTC](https://discourse.gohugo.io/t/variables-inside-javascript/39086/8 "2022-06-14T12:07:19Z")

</div>

Thanks y’all. This works now. Cheers! @bep is the “ExecuteAsTemplate” method still a suitable method?

---

<div class="post-metadata">

**Author:** ![system](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/system/32/1_2.png) [@system](https://discourse.gohugo.io/u/system)\
**Post date:** [June 16, 2022, 12:07pm UTC](https://discourse.gohugo.io/t/variables-inside-javascript/39086/9 "2022-06-16T12:07:58Z")

</div>

Closed on June 16, 2022, 12:07pm UTC

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.
