简体   繁体   English

带有src的浏览器端模板

[英]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.footemplates.bar等来访问各种模板。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 建立在浏览器端的JavaScript组件。 模块化,对JQuery友好 - Browser-side JavaScript components to build on. Modularization, JQuery-friendly JavaScript 事件,用于检测 Windows 上 Firefox 和 IE Edge 中哈希 URL 上的浏览​​器端滚动重新定位 - JavaScript event to detect browser-side scrolling reposition on hash URLs in Firefox and IE Edge on Windows 服务器端的jQuery模板 - jQuery templates on the server side 服务器端模板,客户端模板-自动转换? - Server side templates, client side templates - Automatic conversion? 带有客户端模板的理想解决方案结构 - Ideal Solution structure with client side templates 浏览器检测和Firefox data-src属性 - Browser detection & Firefox data-src attribute 如何更改客户端的 Javascript src 文件? - How do I change the Javascript src file on client side? 单个 HTML 站点仅使用客户端模板和 Rest 调用 - Single HTML site using only client side templates and Rest calls 在Sails.js中使用客户端模板 - Using Client-side Templates in Sails.js 将img src从已经加载到浏览器src中设置,而无需使用jquery再次查询主机 - set img src from already loaded to browser src without querying a host for this again with jquery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM