嵌套数学条件的灰尘模板

时间:2013-09-13 15:55:14

标签: dust.js

我想使用灰尘模板输出每行有4个条目的html表。

这是模板:

var compiledtlist = dust.compile(
            "<table class='table'>" +
            "{#data}" +
            "{@math key=\"{tagid}\" method=\"mod\" operand=\"4\"}" +
            "{@eq value=0}" +
            "<tr><td><button class='btn btn-mini btn-info tag'>{tname}</button>&nbsp;x&nbsp; <span class='badge badge-info'>10</span> <br /><p>{tdesc}</p></td>" +
            "{/eq}" +
            "{@eq value=3}" +
            "<td><button class='btn btn-mini btn-info tag'>{tname}</button>&nbsp;x&nbsp; <span class='badge badge-info'>10</span> <br /><p>{tdesc}</p></td></tr>" +
            "{/eq}" +
            "{@default}" +
            "<td><button class='btn btn-mini btn-info tag'>{tname}</button>&nbsp;x&nbsp; <span class='badge badge-info'>10</span> <br /><p>{tdesc}</p></td>" +
            "{/default}" +
            "{/math}" +
            "{/data}" +
            "</table>"
            , "taglist");
            console.log(compiledtlist);
            dust.loadSource(compiledtlist);
            dust.render("taglist", { data: data }, function (err, out) {
                console.log(err);
                console.log(out);
                $('#tagstable').html(out);
            });

,数据是:

[
   {
      "tagid":10,
      "tname":"sql server",
      "tdesc":"As a database, it is a software product whose primary function is to store and retrieve data as requested by other software applications, be it those on the same computer or those running on another computer across a network (including the Internet)."
   },
   {
      "tagid":9,
      "tname":"entity framework",
      "tdesc":"ADO.NET Entity Framework (EF) is an open source[1] object-relational mapping (ORM) framework for the NET Framework"
   },
   {
      "tagid":8,
      "tname":"spring",
      "tdesc":"he Spring Framework is an open source application framework and inversion of control container for the Java platform"
   },
   {
      "tagid":7,
      "tname":"struts",
      "tdesc":"Apache Struts is an open-source web application framework for developing Java EE web applications. It uses and extends the Java Servlet API to encourage developers to adopt a model–view–controller (MVC) architecture."
   },
   {
      "tagid":6,
      "tname":"asp.net mvc",
      "tdesc":"The ASP.NET MVC Framework is an open source web application framework that implements the model–view–controller (MVC) pattern."
   },
   {
      "tagid":5,
      "tname":"asp.net",
      "tdesc":"ASP.NET is a server-side Web application framework designed for Web development to produce dynamic Web pages. It was developed by Microsoft to allow programmers to build dynamic web sites, web applications and web services."
   },
   {
      "tagid":4,
      "tname":"xml",
      "tdesc":"Extensible Markup Language (XML) is a markup language that defines a set of rules for encoding documents in a format that is both human-readable and machine-readable"
   },
   {
      "tagid":3,
      "tname":"javascript",
      "tdesc":"JavaScript (JS) is an interpreted computer programming language.[5] As part of web browsers, implementations allow client-side scripts to interact with the user, control the browser, communicate asynchronously, and alter the document content that is displayed"
   },
   {
      "tagid":2,
      "tname":"java",
      "tdesc":"Java is a general-purpose, concurrent, class-based, object-oriented computer programming language that is specifically designed to have as few implementation dependencies as possible."
   },
   {
      "tagid":1,
      "tname":"c#",
      "tdesc":"C#[note 1] (pronounced see sharp) is a multi-paradigm programming language encompassing strong typing, imperative, declarative, functional, procedural, generic, object-oriented (class-based), and component-oriented programming disciplines"
   }
]

不幸的是,除了表格标签之外,模板没有返回任何内容。

我在这里缺少什么?

数学标签错了吗?

1 个答案:

答案 0 :(得分:0)

你的代码非常好。问题是,您必须明确要求dustjs-helpers。我相信这应该在文档中正确提及。

  1. 您需要安装dustjs-helpers

    npm install dustjs-helpers
    
  2. 在代码中,您需要此行

    require("dustjs-helpers");
    
  3. 完成这些步骤后,我获得了HTML。

    编辑在linkedin dustjs的回购中提出了一个问题https://github.com/linkedin/dustjs/issues/339