Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

248
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda