use one large external file for many javascript templates with backbone.js?

蹲街弑〆低调 提交于 2019-12-04 13:06:41

I use an extra javascript/coffeescript file and use underscore's templating to take care of everything. This way you can include the templates and put them into as few (or many) files as you would like.

I'm using a single file for all the templates on my current project and it is working out pretty well.

It's a Asp.Net site, so I made the template file into a user control so I can easily include it in any page that uses Backbone.

If you're fetching all templates through AJAX, maybe you don't have to write them to the head.

You can send templates to the client as JSON Object, for example:

{"about":"<p>About</p>...","gallery":"<p>Gallery</p>...","contact":"<p>Contact</p>..."}

After fetching temples you can store them as templates variable inside some object(or locale storage), and after that you can do the following:

var tempStr = templates['about'],
    template = new EJS({element:{value: tempStr, id: 'about'}}),
    content = template.render();
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!