Is this still the case today?
No, you can use @params
to pass any variables to your javascript processed by js.Build
.
How would it work with such a code? I want {{ . }}
to be picked up by the JS.
{{- with site.Params.google.analytics }}
<script >
window.minimalAnalytics = {
trackingId: '{{ . }}',
autoTrack: true, // <-- init tracking
};
</script>
{{- end }}
Edit: This worked. But I am still interested to know if/how the @params
method would work.
Something like this:
assets/script.js:
import params from "@params";
window.minimalAnalytics = {
trackingId: params.trackingId,
autoTrack: true, // <-- init tracking
};
In HTML template:
{{- $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
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”.
(() => {
// <stdin>
window.minimalAnalytics = {
trackingId: "params.trackingId",
autoTrack: true
};
})();
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
.
Sorry, fixed.
Thanks y’all. This works now. Cheers! @bep is the “ExecuteAsTemplate” method still a suitable method?
This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.