Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

249
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda