npm install swig
<h1>{{ pagename|title }}</h1> <ul> {% for author in authors %} <li{% if loop.first%} class="first"{% endif %}> {{ author }} </li> {% else %} <li>There are no authors.</li> {% endfor %} </ul>
var template = require('swig'); var tmpl = template.compileFile('/path/to/template.html'); tmpl.render({ pagename: 'awesome people', authors: ['Paul', 'Jim', 'Jane'] });
<h1>Awesome People</h1> <ul> <li class="first">Paul</li> <li>Jim</li> <li>Jane</li> </ul>
swig.compileFile
you should always use swig.compile
extends
, import
and include
, you must specify the templateKey
: swig.compile(templateString, { filename: templateKey });
<script type='text/javascript" src="//path/to/swig/swig.js">
var template = swig.compile('<p>{% block content %}{% endblock %}</p>', { filename: 'main' }); var mypage = swig.compile('{% extends "main" %}{% block content %}Oh hey there!{% endblock %}', { filename: 'mypage' });
console.log(mypage.render({}));
<p>Oh hey there!</p>
Source: https://habr.com/ru/post/174207/
All Articles