Nunjucks
Il existe de nombreux moteurs de template node.js, mais nunjucks peut être écrit en python. Voir ici pour une documentation détaillée. https://mozilla.github.io/nunjucks/
Presque le même que Jinja2. http://jinja.pocoo.org/docs/dev/
$ npm install nunjucks --save
app.js
var express = require('express');
var path = require('path');
var nunjucks = require('nunjucks');
var app = express();
// nunjucks view engine
nunjucks.configure('views', {
autoescape: true,
express: app
});
// setup view engine
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'html');
Si vous utilisez ʻexpress, spécifiez-le dans
nunjucks.configure.
view engine est maintenant
html`.
{% set x = 5 %}
{{ x }}
{% if variable %}
It is true
{% endif %}
{% set items = [{"title": "a"}, {"title": "b"}] %}
{% for item in items %}
<p>{{ item.title }}</p>
{% endfor %}
Essayez d'utiliser une bibliothèque appelée nunjucks-date-filter. Il semble que moment soit utilisé comme modèle.
$ npm install nunjucks-date-filter --save
$ npm install consolidate --save
app.js
var express = require('express');
var path = require('path');
var nunjucks = require('nunjucks');
var dateFilter = require('nunjucks-date-filter');
var cons = require('consolidate');
var app = express();
// nunjucks view engine
cons.requires.nunjucks = require('nunjucks');
var env = cons.requires.nunjucks.configure('views', {
autoescape: true,
express: app
});
// date format
dateFilter.setDefaultFormat('YYYY-MM-DD');
env.addFilter('date', dateFilter);
// setup view engine
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'html');
// with no format
This blog has been created at {{ creation_date | date }}.
// with a custom format
This blog has been created at {{ creation_date | date("YYYY") }}.
// with an addition first
This blog has been created at {{ creation_date | date("add", 7, "days") | date }}.
Recommended Posts