雨果:JS文件中的访问模板变量

发布于 2025-02-06 15:37:51 字数 425 浏览 1 评论 0原文

在我的Hugo网站上,BaseUrl设置为:weblity.com/variable。

如果图像元素在.html(< img src =“/images/images/images.png”>),则应用baseurl,最终URL是:weblity.com/variable/images/images/image/image.png/

if图像元素在.js(img.src =“/images/image.png”)中,baseurl不应用,最终URL不正确:werfebal.com/images/images/images/image.png/

我尝试添加一个自定义config.toml中JS的输出格式,但它不起作用: [Mediatypes。“ Application/JavaScript”] 后缀= [“ JS”]

是否有办法访问.js文件中的模板变量?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小糖芽 2025-02-13 15:37:51

您可以做类似的事情:

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

然后在您的js文件中:

import * as params from '@params';

例如,我在一个网站中有一个:

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

然后在Analytics-msftclarift.js文件中,我有:

import * as params from '@params';

…

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

请参阅Hugo Docs javascript构建有关更多信息。

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.

人海汹涌 2025-02-13 15:37:51

回答您的问题:
“有没有办法访问.js文件中的模板变量?”
不,正如雨果建造的那样,雨果的模板代码不再存在。 JS在构建站点上运行。

回答您的另一个问题:
“如果图像元素在.html中(&lt; img src =“/images/images.png”&gt;),则应用baseurl,最终URL是:weblity.com/variable/images/images/images/image.png/ “

”如果图像元素在.js(img.src =“/images/image.png”)中,则不会应用baseurl,最终URL不正确:werfebal.com/images/images/image.png/

我会建议您使用图像处理并生成完整链接,然后您可以访问/无需处理图像链接,但是我需要有关您的设置的更多数据。希望这能给您足够的工作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文