2012-03-27 2 views
11

Итак, я только начал работать с flatironjs и «plates». Я пытаюсь понять, как у меня может быть основной шаблон макета, а затем частичный шаблон, который загружает контент в основной шаблон макета, аналогичный тому, как это делает expressjs ...flatiron.js/пластины частичные шаблоны?

С expressjs есть layout.js и, возможно, index.js. index.js заполняет область содержимого layout.js. Похоже, это будет испечено. Я не вижу способа сделать это на основе документации.

ответ

16

Основной шаблон макета (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> 

+4

^Отличный ответ. Весь код, без пуха –