如何将我的胡子/车把模板存储在单独的文件中?
我在一个项目中使用handlebars.js,并且开始拥有相当数量的模板。 目前它们存储在我的主模板应用程序文件中,如下所示:
<script id="avatar_tpl" type="text/html">
bla bla bla {{var}} bla bla bla
</script>
我想知道是否有一种方法可以将它们放入单独的文件(例如 .js 文件或其他文件)中,以避免将它们堆叠在我的源代码页面中。
我知道有几种解决方案可以通过 Ajax 调用这些模板,但这似乎会给我带来太多不必要的请求。
谢谢
I'm using handlebars.js on a project and I'm starting to have a fair amount of templates.
For now they are stored in my main template app file, like this :
<script id="avatar_tpl" type="text/html">
bla bla bla {{var}} bla bla bla
</script>
I'm wondering if there is a way to put them in a separate file like a .js file or something, to avoid stacking them up in my source code page.
I'm aware that there are several solutions to call theses templates via Ajax, but that seems to result in too much unnecessary requests for me.
Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我创建并开源了 NodeInterval 来解决我的 HTML 页面中太多 js 模板的完全相同的问题。
它允许您将所有模板放入按您喜欢的任何层次结构组织的模板文件夹中。它具有内置的
watch
功能,因此当您修改任何这些模板时,它会自动更新您的 HTML 页面。我将它与 SASS 一起用于我的 CSS。我每天都将它与下划线模板一起使用,但它也应该与胡子模板一起正常工作:
https://github.com/krunkosaurus/NodeInterval
I created and open-sourced NodeInterval for this exact same problem of too many js templates in my HTML page.
It allows you to put all your templates into a templates folder organized in whatever hierarchy you like. It has a built in
watch
capability so that as you modify any of these templates it automatically updates your HTML page. I use it alongside SASS for my CSS.I use it daily with underscore templates but it should work fine with moustache templates as well:
https://github.com/krunkosaurus/NodeInterval
难道你不能只在模板中包含一个 js 文件作为 js 变量吗?没有测试过,只是在这里思考:
Couldn't you just include a js file with your templates as js variables? Not tested, just thinking here:
如果您使用jquery,您可以创建一个ID为“template-holder”的不可见div
,然后使用:
将html加载到div中
然后使用 :
获取模板
:)
if you are using jquery, you could create an invisible div with id "template-holder"
then use :
to load the html into the div
then use :
to get the template
:)
我不熟悉handlebars.js,但是,你尝试过这个吗?:
I'm not familiar with handlebars.js but, have you tried this?:
我现在已经将多个项目的所有脚本和模板都汇总到一个大的 .js 文件中。我使用基于 java 的构建工具 ant 来连接和管理我的 js 的各种处理脚本。
在 JavaScript 变量中存储大型模板的最大问题是 JavaScript 缺乏多行字符串。我通过使用类似 python 的三引号语法编写文件来处理这个问题:
然后,我通过下面包含的 python 脚本运行这个自定义语法 javascript 文件,这会将其转换为合法的 javascript:
通过这种方式工作,我可以编写模板在或多或少的纯 html 中,并将它们与应用程序代码一起部署在单个 .js 文件中。
I've been rolling all my scripts and templates in to one big .js file for several projects now. I use a java-based build tool, ant, to concatenate and manage various processing scripts for my js.
The biggest problem with storing large templates in javascript variables is javascript's lack of multi-line strings. I deal with this by writing my files with a python-like triple-quote syntax:
I then run this custom-syntax javascript file though the python script included below, which turns it in to legal javascript:
Working this way, I can code templates in more-or-less pure html, and deploy them, along with application code, inside a single .js file.
我创建了一个延迟加载 JavaScript 文件,仅根据需要加载模板。它正在执行 AJAX 调用,但似乎工作得很好。
I created a Lazy Load javascript file that loads the templates only as needed. It's performing the AJAX calls, but seems to work quite well.