Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
298 views
in Technique[技术] by (71.8m points)

javascript - How to outsource a template js to a different file when using Handlebars.js

I have this template script

<script id="some-template" type="text/x-handlebars-template">
   {{#users}}
     {username}
     {email}
</script>

I want to out-source it to a file called "user_template.js" which will look like this:

   {{#users}}
     {username}
     {email}

and make in the main index.html this link:

<script id="some-template" type="text/x-handlebars-template" src="user_template.js"></script>

The problem is - it doesn't work - how do I do it?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I would use RequireJS for this. Lovely to work with modules and there is also a plugin that is called text that works beautifully with templates.

If it sounds interesting, here's some links:

http://requirejs.org/

http://requirejs.org/docs/download.html#text -- The text plugin

RequireJS is only suitable if you want to use modules though, if not Alon's answer is better.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...