gpt4 book ai didi

使用 Plates 进行 Javascript (nodejs) 模板化

转载 作者:太空宇宙 更新时间:2023-11-04 02:23:09 26 4
gpt4 key购买 nike

我想在服务器端构建我的 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 模板库建议,这些建议确实像这个一样工作(如果这解决了我的问题)...

最佳答案

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

您需要使用id属性,并将其更改为 <p id="my-content">使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));

关于使用 Plates 进行 Javascript (nodejs) 模板化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31968416/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com