Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

247
Views
Hugo: Access template variables in JS files

In my Hugo website, baseUrl is set to: website.com/variable.

If an image element is in .html (< img src="/images/image.png" >), the baseUrl is applied and the final url is this: website.com/variable/images/image.png/

If an image element is in .js (img.src = "/images/image.png"), the baseUrl is not applied and the final url is incorrect: website.com/images/image.png/

I tried to add a custom output format for js in config.toml, but it didn't work: [mediaTypes."application/javascript"] suffixes = ["js"]

Is there a way to access template variables in .js files?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

To answer your question: "Is there a way to access template variables in .js files?" No, as Hugo builds, then Hugo's template codes no longer exist. JS runs on the built site.

To answer your other question: "If an image element is in .html (< img src="/images/image.png" >), the baseUrl is applied and the final url is this: website.com/variable/images/image.png/"

"If an image element is in .js (img.src = "/images/image.png"), the baseUrl is not applied and the final url is incorrect: website.com/images/image.png/"

I would suggest you use image processing and generate the full links, and then you can access/no deal with your image links, but I would need more data about your set-up. Hopefully that gives you enough to work with.

about 4 years ago · Juan Pablo Isaza Report

0

You can do something like this:

{{ $js := resources.Get "js/main.js" | js.Build (dict "params" (dict "api" "https://example.org/api")) }}

Then in your JS file:

import * as params from '@params';

For example, I have this in one of my sites:

{{- $msftclarityjs := resources.Get "js/analytics-msftclarity.js" | js.Build ( dict "params" ( dict "msftclarityid" $msftclarityid ) ) -}}

Then in the analytics-msftclarift.js file I have this:

import * as params from '@params';

…

(window, document, "clarity", "script", params.msftclarityid);

See Hugo docs JavaScript Building for more info.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!