【问题标题】:Keep beautified indentation structure when putting html into a variable?将html放入变量时保持美化的缩进结构?
【发布时间】:2013-01-30 16:13:26
【问题描述】:

从编码开始,JS,今天多亏了 JS,我在 HTML 中多次注入了一个 HTML 模板。但是,我对缩小 html 缩进并将所有十几个行(div、h1、span、table、tr、td、tt)折叠成单个 var 行的系统需求感到困惑。缩小的 html 既难以准备好,也经常需要在测试阶段进行美化。

另外:有没有办法将美化缩进的 html 保留在变量中?

一些具有有效版本的语法:

var exampleTpl = "
      <div data-demo-html="true">
        <div data-role="collapsible-set" data-inset="true" data-theme="b" data-content-theme="d">
          <div data-role="collapsible" data-collapsed="false">
            <h4>Heading</h4>
            <ul data-role="listview" data-filter="false" data-inset="true">
              <li><a href="#">List item 1</a></li>
              <li><a href="#">List item 2</a></li>
              <li><a href="#">List item 3</a></li>
            </ul>
          </div>
"

提前致谢,

【问题讨论】:

    标签: javascript templates handlebars.js var mustache


    【解决方案1】:

    出于模板的目的,您应该查看Handlebars.js homepage 上的示例:

    声明你的模板

    <script id="entry-template" type="text/x-handlebars-template">
      <div class="entry">
        <h1>{{title}}</h1>
        <div class="body">
          {{body}}
        </div>
      </div>
    </script>
    

    在 JavaScript 中编译模板

    var source   = $("#entry-template").html();
    var template = Handlebars.compile(source);
    

    扩充 HTML

    var context = {title: "My New Post", body: "This is my first post!"}
    var html    = template(context);
    

    这将产生一个你的模板实例(值注入你的模板):

    <div class="entry">
      <h1>My New Post</h1>
      <div class="body">
        This is my first post!
      </div>
    </div>
    

    【讨论】:

      【解决方案2】:

      你可以这样做:

      var exampleTpl =
            '<div data-demo-html="true">' + 
              '<div data-role="collapsible-set" data-inset="true" data-theme="b" data-content-theme="d">' +
                '<div data-role="collapsible" data-collapsed="false">' +
                  '<h4>Heading</h4>' +
                  '<ul data-role="listview" data-filter="false" data-inset="true">' +
                    '<li><a href="#">List item 1</a></li>' +
                    '<li><a href="#">List item 2</a></li>' +
                    '<li><a href="#">List item 3</a></li>' +
                  '</ul>' +
                '</div>' +
              '</div>' +
            '</div>';
      

      如果你真的想要...

      但是,如果您在 mustache JS 中表示模板(只需查看您的标签),我个人将模板添加到脚本元素中,然后按照某些模板库的建议使用 innerHTML 访问它们(例如handlebarsjs.com)。

      例如:

          <script type="text/html" id="myTemplate">
           <div data-demo-html="true">
              <div data-role="collapsible-set" data-inset="true" data-theme="b" data-content-theme="d">
                <div data-role="collapsible" data-collapsed="false">
                  <h4>Heading</h4>
                  <ul data-role="listview" data-filter="false" data-inset="true">
                    <li><a href="#">List item 1</a></li>
                    <li><a href="#">List item 2</a></li>
                    <li><a href="#">List item 3</a></li>
                  </ul>
                </div>
              </div>
            </div>
          </script>
      

      【讨论】:

      • 我想知道是否有一个完美的语法,不需要'+',会很酷。也谢谢你的诡计
      • @Hugolpz 我已经更新了答案。这更像是你所希望的吗?你真的在使用 mustache JS 吗?
      • 我建议不要将 type="text/html" 与模板一起使用,浏览器可能会尝试将模板解释为 HTML 并造成混乱。
      • @muistooshort 过去并没有给我带来任何问题,但我看到车把已经更新了他们的示例以使用 text/x-handlebars-template - 所以这可能是一个不错的选择。更适合语义。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-21
      相关资源
      最近更新 更多