gpt4 book ai didi

node.js - flatiron.js/plates 部分模板?

转载 作者:IT老高 更新时间:2023-10-28 23:15:45 24 4
gpt4 key购买 nike

所以,我刚开始使用 flatironjs 和“plates”。我试图弄清楚如何拥有一个主布局模板,然后是一个将内容加载到主布局模板中的部分模板,类似于 expressjs 的做法......

对于 expressjs,有 layout.js,也许还有 index.js。 index.js 填充 layout.js 的内容区域。看起来这会被烘烤我没有看到基于文档的方法。

最佳答案

主布局模板(template.html):

<h1>This is the main template.</h1>
<div id="main"></div>

部分(partial.html):

<p>This is the partial that should be rendered into the main template.</p>

那么你可以这样做:

var fs = require("fs"),
Plates = require("plates");

// Read the two files from disk

var template = fs.readFileSync("template.html", "utf-8");
var partial = fs.readFileSync("partial.html", "utf-8");

// Render the partial into main.
// The data-key in the second param is matched to the id in the template.
// Plates renders the corresponding value - in this case the contents of
// partial.html - between the start and end tags with this id.

var rendered = Plates.bind(template, {main: partial});

所以console.log(rendered)应该给你:

<h1>This is the main template.</h1>
<div id="main">
<p>This is the partial that should be rendered into the main template.
</p>

关于node.js - flatiron.js/plates 部分模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9892496/

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