2015年8月26日 星期三

ejs example

from :
https://scotch.io/tutorials/use-ejs-to-template-your-node-application


app.get('/', function(req, res) {
    var drinks = [
        { name: 'Bloody Mary', drunkness: 3 },
        { name: 'Martini', drunkness: 5 },
        { name: 'Scotch', drunkness: 10 }
    ];
    var tagline = "Any code of your own that you haven't looked at for six or more months might as well have been written by someone else.";

    res.render('pages/index', {
        drinks: drinks,
        tagline: tagline
    });
});
<h2>Variable</h2>
<p><%= tagline %></p>
...<h2>Loop</h2>
<ul>
    <% drinks.forEach(function(drink) { %>
        <li><%= drink.name %> - <%= drink.drunkness %></li>
    <% }); %>
</ul>

===========  if   ==========
app.get("/recipes", function(req, res) {
    res.render("recipes.ejs", {
        recipes: recipes
    });
});
recipes.ejs File:
<%if (recipes.length > 0) { %>
// Do something with more than 1 recipe
<% } %>

沒有留言:

張貼留言