Shopify 的 {% javascript %} 标签是如何工作的?

How does Shopify's {% javascript %} tag work?

我无法找到有关此标签如何工作的任何信息。为什么我应该使用 {% javascript %} 而不是 HTML 脚本标签?有什么不同吗?我检查了描述所有其他标签的 Shopify cheatsheet,并筛选了 google 的几页,但没有发现任何实质性内容。

这只是为您提供方便。它没有为您提供任何特殊功能。这很好,因为对于不太出色的技术人员来说,他们可能会得到一点 Liquid 而不是其他很多,很明显标签内的东西很可能 Javascript。他们建议您仅在您的部分用于广泛分布的主题和/或许多商店时才使用这些标签。换句话说,如果您喜欢脚本标签,请继续使用它们,没有坏处。

如果您要在主题 section 上下文中添加 {% javascript %} 标签,则:

Sections can bundle their own script and style assets using the javascript and stylesheet tags. You should only need to use this for sections that are meant to be reused or installed on multiple themes or shops.

The scripts for all sections are concatenated into a single file by Shopify and injected into content_for_header. The injected <script> is set to load asynchronously with the defer attribute. The javascript tag is wrapped in a self-executing anonymous function and a try/catch, which means that variables get defined inside a closure and runtime exceptions won't affect other sections.

Reference