Skip to content

Commit 25a1cca

Browse files
committed
add use-nunjucks project
1 parent 1211077 commit 25a1cca

File tree

6 files changed

+113
-0
lines changed

6 files changed

+113
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
{
5+
"name": "Start",
6+
"type": "node",
7+
"request": "launch",
8+
"program": "${workspaceRoot}/app.js",
9+
"stopOnEntry": false,
10+
"args": [],
11+
"cwd": "${workspaceRoot}",
12+
"preLaunchTask": null,
13+
"runtimeExecutable": null,
14+
"runtimeArgs": [
15+
"--nolazy"
16+
],
17+
"env": {
18+
"NODE_ENV": "development"
19+
},
20+
"externalConsole": false,
21+
"sourceMaps": false,
22+
"outDir": null
23+
}
24+
]
25+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
const nunjucks = require('nunjucks');
2+
3+
function createEnv(path, opts) {
4+
var
5+
autoescape = opts.autoescape && true,
6+
noCache = opts.noCache || false,
7+
watch = opts.watch || false,
8+
throwOnUndefined = opts.throwOnUndefined || false,
9+
env = new nunjucks.Environment(
10+
new nunjucks.FileSystemLoader('views'), {
11+
autoescape: autoescape,
12+
noCache: noCache,
13+
watch: watch,
14+
throwOnUndefined: throwOnUndefined
15+
});
16+
if (opts.filters) {
17+
for (var f in opts.filters) {
18+
env.addFilter(f, opts.filters[f]);
19+
}
20+
}
21+
return env;
22+
}
23+
24+
var env = createEnv('views', {
25+
watch: true,
26+
filters: {
27+
hex: function (n) {
28+
return '0x' + n.toString(16);
29+
}
30+
}
31+
});
32+
33+
var s = env.render('hello.html', {
34+
name: '<Nunjucks>',
35+
fruits: ['Apple', 'Pear', 'Banana'],
36+
count: 12000
37+
});
38+
39+
console.log(s);
40+
41+
console.log(env.render('extend.html', {
42+
header: 'Hello',
43+
body: 'bla bla bla...'
44+
}));
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "use-nunjucks",
3+
"version": "1.0.0",
4+
"description": "Test nunjucks",
5+
"main": "app.js",
6+
"scripts": {
7+
"start": "node app.js"
8+
},
9+
"keywords": [
10+
"nunjucks",
11+
"templating"
12+
],
13+
"author": "Michael Liao",
14+
"license": "Apache-2.0",
15+
"repository": {
16+
"type": "git",
17+
"url": "https://github.com/michaelliao/learn-javascript.git"
18+
},
19+
"dependencies": {
20+
"nunjucks": "2.4.2"
21+
}
22+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<html><body>
2+
{% block header %} <h3>Unnamed</h3> {% endblock %}
3+
{% block body %} <div>No body</div> {% endblock %}
4+
{% block footer %} <div>copyright</div> {% endblock %}
5+
</body>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{% extends 'base.html' %}
2+
3+
{% block header %}<h1>{{ header }}</h1>{% endblock %}
4+
5+
{% block body %}<p>{{ body }}</p>{% endblock %}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<html>
2+
<head>
3+
<title>Hello {{ name }}</title>
4+
</head>
5+
<body>
6+
<h3>Fruits List</h3>
7+
{% for f in fruits %}
8+
<p>{{ f }}</p>
9+
{% endfor %}
10+
<p>Total: {{ count|hex }}</p>
11+
</body>
12+
</html>

0 commit comments

Comments
 (0)