2012-03-27 13 views
11

So begann ich gerade mit flatironjs und "plates" zu arbeiten. Ich versuche herauszufinden, wie ich eine Hauptlayoutvorlage haben kann und dann eine Teilvorlage, die Inhalt in die Hauptlayoutvorlage ähnlich wie expressjs lädt ...flatiron.js/Platten teilweise Vorlagen?

Mit expressjs gibt es die layout.js und vielleicht index.js. index.js füllt den Inhaltsbereich von layout.js auf. Es scheint, als wäre dies gebacken, ich sehe keinen Weg, dies basierend auf der Dokumentation zu tun.

Antwort

16

Haupt Layout-Vorlage (template.html):

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

Teil (partial.html):

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

Dann können Sie dies tun:

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}); 

So console.log(rendered) sollte Geben Sie:

<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

^Große Antwort. Alles Code, kein Flaum –