【问题标题】:Javascript (nodejs) templating with Plates使用 Plates 进行 Javascript (nodejs) 模板化
【发布时间】:2015-08-12 14:40:46
【问题描述】:

我想在服务器端构建我的 html 代码。

为此,我使用了 Plates,它是一个模板化的 JavaScript 库。
我试图让这段代码工作,但它只是不修改原始的 html 模板代码......

var Plates = require("plates")

var html    = "<h1>Template test with plates</h1>"
            + "<p my-content>This should be erased...</p>";

var data = { 'my-content': "It is: "+new Date() };

console.log(Plates.bind(html, data));

这应该输出日期而不是默认字符串,但它不会...

我也尝试过使用映射器,但它没有成功:

var Plates = require("plates")

var html    = "<h1>Template test with transparency</h1>"
            + "<p my-content>This should be erased...</p>";

var data = { 'content': "It is: -> "+new Date() };

var map = Plates.Map();
map.where('my-content').use('content');

console.log(Plates.bind(html, data, map));

我得到的唯一代码示例如下:

var Plates = require("plates")

var html    = "<h1>Template test with transparency</h1>"
            + "<p my-content='changeable'>This should be erased...</p>";

var data = { 'content': "It is: -> "+new Date() };

var map = Plates.Map();
map.where('my-content').is("changeable").use('content');

console.log(Plates.bind(html, data, map));

但我不需要my-content='changeable' 标签,我只需要my-content 标签...

有人可以帮我吗?

我也接受其他与此类似的 nodejs 模板库建议(如果这解决了我的问题)...

【问题讨论】:

    标签: javascript node.js html templates


    【解决方案1】:

    您的代码不起作用,因为您使用my-content 作为属性。

    您需要使用id 属性,并将其更改为&lt;p id="my-content"&gt; 以使bind() 起作用。

    例如

    var Plates = require("plates")
    
    var html    = '<h1>Template test with plates</h1>'
                + '<p id="my-content">This should be erased...</p>';
    
    var data = { 'my-content': "It is: "+new Date() };
    
    console.log(Plates.bind(html, data));
    

    【讨论】:

    • 是的,我同意,但在我的第三个代码示例中,我使用 my-content 作为具有值的属性(在这种情况下它工作正常)......而且我需要使用用于模板化我的 HTML 的属性(没有属性值)......难道没有使用“板”的解决方案,或者可能会有一个类似的模板引擎允许我根据无价值的 html 属性模板化我的 HTML 吗?
    猜你喜欢
    • 1970-01-01
    • 2011-09-25
    • 2019-02-28
    • 2011-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-02
    • 2020-09-30
    相关资源
    最近更新 更多