Skip to content

Instantly share code, notes, and snippets.

@satosystems
Last active November 10, 2015 01:05
Show Gist options
  • Save satosystems/e9cfcac1cfb7a31255cf to your computer and use it in GitHub Desktop.
Save satosystems/e9cfcac1cfb7a31255cf to your computer and use it in GitHub Desktop.
node.js で使えるテンプレートエンジン ref: http://qiita.com/satosystems/items/cf7b748c0f95c95aa48b
<% if (user) { %>
<h2><%= user.name %></h2>
<% } %>
<ul>
<% users.forEach(function(user){ %>
<%- include('user/show', {user: user}); %>
<% }); %>
</ul>
var swig = require('swig');
swig.renderFile('/path/to/template.html', {
pagename: 'awesome people',
authors: ['Paul', 'Jim', 'Jane']
});
var swig = require('swig');
swig.renderFile('/path/to/template.html', {
pagename: 'awesome people',
authors: ['Paul', 'Jim', 'Jane']
});
<h1>Awesome People</h1>
<ul>
<li class="first">Paul</li>
<li>Jim</li>
<li>Jane</li>
</ul>
<h1>Awesome People</h1>
<ul>
<li class="first">Paul</li>
<li>Jim</li>
<li>Jane</li>
</ul>
<div class="timeline">
<!-- load more button -->
<button>{{message}}</button>
<!-- tweet object -->
{{#tweets}}
{{> tweet}}
{{/tweets}}
</div>
<h1>{{header}}</h1>
{{#bug}}
{{/bug}}
{{#items}}
{{#first}}
<li><strong>{{name}}</strong></li>
{{/first}}
{{#link}}
<li><a href="{{url}}">{{name}}</a></li>
{{/link}}
{{/items}}
{{#empty}}
{
"header": "Colors",
"items": [
{"name": "red", "first": true, "url": "#Red"},
{"name": "green", "link": true, "url": "#Green"},
{"name": "blue", "link": true, "url": "#Blue"}
],
"empty": true
}
<h1>Colors</h1>
<li><strong>red</strong></li>
<li><a href="#Green">green</a></li>
<li><a href="#Blue">blue</a></li>
<p>The list is empty.</p>
<h1>Colors</h1>
<li><strong>red</strong></li>
<li><a href="#Green">green</a></li>
<li><a href="#Blue">blue</a></li>
<p>The list is empty.</p>
<div class="entry">
<h1>{{title}}</h1>
<div class="body">
{{body}}
</div>
</div>
{
title: "My New Post",
body: "This is my first post!"
}
<div class="entry">
<h1>My New Post</h1>
<div class="body">
This is my first post!
</div>
</div>
<div class="entry">
<h1>My New Post</h1>
<div class="body">
This is my first post!
</div>
</div>
<h1>{{ pagename|title }}</h1>
<ul>
{% for author in authors %}
<li{% if loop.first %} class="first"{% endif %}>
{{ author }}
</li>
{% endfor %}
</ul>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment