预编译小胡子模板还是从外部加载?
拥有一个 Coffeescript include 函数会很有用,这样它就可以在用 JavaScript 编译时加载外部胡子模板,并且不会弄乱咖啡文件。
实际上,您可以在运行时加载 .mustache 文件,但您需要使用 ajax 请求来调用它们,这会涉及一些性能损失。
我想预编译一些静态胡子模板并将它们包含在生成的 javascript 函数中,该函数可以 缝合 并压缩在一个文件。
有相关的项目或脚本吗?
It would be useful to have a Coffeescript include function so it could load the external mustache templates when compiling in javascript and not clutter the coffee files.
Actually you can load .mustache files at runtime but you need to call them with an ajax request with some performance penalities involved.
I would like to precompile some static mustache templates and include them in generated javascript function that could be Stitched and compressed in a single file.
Is there a project or a script for that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我认为这个解决方案适合您,用于 Mustache 和其他模板引擎的 javascript 模板预编译器 https://github.com/kupriyanenko/jsttojs
例如,与命令行一起使用
或使用 grunt 解决方案,grunt-jsttojs
I think this solution for you, javascript template precompiller for mustache and othe template engines https://github.com/kupriyanenko/jsttojs
for example, use with command line
or use solution for grunt, grunt-jsttojs
首先,您可以使用这样的方法:
将模板包含在专用脚本块中,而不是作为代码中的字符串。
getElementByID()
+innerHtml()
将为您提供可以使用的源代码。关于 Mustache 一般而言 - 严格来说你无法编译它。每次“实例化”模板时,模板都会被解释。
如果您确实需要编译它们,请考虑使用我的 Kite 引擎:
http://code.google.com/p/kite/ 或任何其他可编译模板:
http://jsperf.com/dom-vs-innerhtml-based-templated/99
First of all you can use something like this:
to include your templates in dedicated script blocks rather than as strings in code.
getElementByID()
+innerHtml()
will give you its source that you can use.About the Mustache in general - you cannot compile it strictly speaking. Templates get interpreted each time you "instantiate" the template.
If you do need to compile them then consider use of my Kite engine:
http://code.google.com/p/kite/ or any other compileable templates:
http://jsperf.com/dom-vs-innerhtml-based-templating/99
当然,这就是我们在我工作的地方所做的事情。所有模板都放在一个 html 文件中,并在构建时插入到 dom 中。每个模板都存储在未知类型的脚本标记中,因此浏览器会忽略它。然后您可以使用选择器引用它们。
John Resig 有一篇关于该技术的好文章 http://ejohn.org/blog/javascript-micro -模板/
Absolutely, this is something we do where I work. All the templates go in a single html file and get's inserted into the dom at build time. Each template is stored in a script tag of type unknown so the browser just ignores it. Then you can reference them using selectors.
John Resig has a good article on the technique http://ejohn.org/blog/javascript-micro-templating/
我正在考虑做类似的事情。我还没有尝试过,但似乎你可以使用 Node.js 和 Mu,小胡子为 Node.js 构建,以执行此操作。伪JS代码...
I'm looking at doing something similar. I haven't tried this yet, but it seems like you could use Node.js and Mu, the Mustache build for Node.js, to do this. Pseudo-JS code...
Twitter 的库 Hogan.js 可以完成这项工作。
Twitter's library Hogan.js does the job.
您可以 渲染直接包含的模板字符串在您的源代码中使用 Mustache.to_html() ,如果有帮助的话。
You can render a template string included directly in your source using Mustache.to_html(), if that helps.