雨果:JS文件中的访问模板变量
在我的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以做类似的事情:
然后在您的js文件中:
例如,我在一个网站中有一个:
然后在Analytics-msftclarift.js文件中,我有:
请参阅Hugo Docs javascript构建有关更多信息。
You can do something like this:
Then in your JS file:
For example, I have this in one of my sites:
Then in the analytics-msftclarift.js file I have this:
See Hugo docs JavaScript Building for more info.
回答您的问题:
“有没有办法访问.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.