[英]Browser-Side templates with src
Recently I've been exploring Backbone , and they do encourage using a template engine. 最近我一直在探索Backbone ,他们鼓励使用模板引擎。
Now, I've got a bunch of html files that I want to display using that, and I will most likely have to edit them often in the development stage. 现在,我有一堆html文件,我想用它来显示,我很可能不得不在开发阶段经常编辑它们。 The most tutorials I read recomment something like this:
我阅读的大多数教程都推荐如下:
<script type="text/template" id="template1">
<ul>
<li>Hic sunt dracones</li>
</ul>
</script>
<script type="text/javascript">
template = $('#template1').html();
<script>
So I'd like to know if this can be made to work with the src
-attribute to include the template files. 所以我想知道是否可以使用
src
-attribute来包含模板文件。 Or, if not, what is the usual approach to load template files? 或者,如果没有,加载模板文件的常用方法是什么?
Using src
for this is not possible. 使用
src
是不可能的。 You could include a JavaScript file though which is generated on the server from separate template files; 您可以包含一个JavaScript文件,通过该文件在服务器上从单独的模板文件生成; with PHP it could be as simple as this:
使用PHP它可以像这样简单:
<?php
$templates = array(
'foo' => file_get_contents('foo.html'),
'bar' => file_get_contents('bar.html'),
);
header('Content-type: text/javascript');
echo 'var templates = ' . json_encode($templates);
?>
Loading this file with a <script>
tag then gives you an object templates
where you can acccess the various templates via templates.foo
, templates.bar
, etc. 使用
<script>
标记加载此文件,然后为您提供一个对象templates
,您可以通过templates.foo
, templates.bar
等来访问各种模板。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.