预编译为纯 JavaScript 的 JavaScript 模板语言

发布于 2024-10-07 06:50:14 字数 647 浏览 0 评论 0原文

我在 Google Closure 编译器中使用google.com/closure/compiler/docs/api-tutorial3.html" rel="nofollow">高级模式适用于我正在处理的项目。这是一个极其激进的 JavaScript 编译器,可以大大优化你的代码。然而,为了做到这一点,它需要看到整体情况。也就是说,如果我给window.foo分配一些东西,然后只从编译器看不到的JS访问window.foo,它就不会存在,因为它会被更改为window.f甚至被删除全部在一起。

当涉及到 JS 寺庙时,这会让事情变得复杂。由于它们是在运行时从字符串中加载的,因此它们往往会引用不再存在的变量。通过对用于向模板提供值的对象使用带引号的赋值,可以稍微缓解这一问题。例如。 templateContext['user'] 而不是 templateContext.user。但是,这不允许我执行 templateContext.user.username 等操作,因此它有些限制。

tl;dr:我需要一个 JavaScript 模板解决方案,可以将其预处理为纯 JavaScript,然后将其包含在 .js 文件中。

有这样的事情存在吗?

I'm using the Google Closure Compiler in advanced mode for a project I'm working on. This is an extremely aggressive JavaScript compiler that can optimize your code quite a bit. However, in order to do this it needs to see the whole picture. That is to say, if I assign something to window.foo, and then only access window.foo from JS that the compiler can't see, it won't exist, since it will have been changed to window.f or even removed all together.

This complicates things when it comes time for JS temples. Since they are loaded at runtime out of a string, they tend to refer to variables that no longer exist. This can be mitigated slightly by using quoted assignment for the object used to provide values to the template. eg. templateContext['user'] rather then templateContext.user. However, that doesn't allow me to do templateContext.user.username and so on, so it's somewhat limiting.

tl;dr: I need a JavaScript templating solution that can be pre-processed into pure JavaScript that I can include in a .js file.

Does anything like this exist?

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

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

发布评论

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

评论(2

那支青花 2024-10-14 06:50:14

老实说,我不确定我是如何错过这个的:Google Closure Templates

Honestly, I'm not sure how I missed this: Google Closure Templates.

这个俗人 2024-10-14 06:50:14

您可以尝试Dojo 1.6。有一种方法可以让它在高级模式下与 Closure 无缝协作,甚至使用 Dijit 模板也是如此。

http://dojo-toolkit.33424 .n3.nabble.com/file/n2636749/Using_the_Dojo_Toolkit_with_the_Closure_Compiler.pdf?by-user=t

You can try Dojo 1.6. There is a way to make it work quite seamlessly with Closure in Advanced mode, even with Dijit templates.

http://dojo-toolkit.33424.n3.nabble.com/file/n2636749/Using_the_Dojo_Toolkit_with_the_Closure_Compiler.pdf?by-user=t

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