feat: make templates extend common layout
Alan Pearce alan@alanpearce.eu
Sun, 18 Jun 2017 22:12:48 +0200
7 files changed, 36 insertions(+), 1 deletions(-)
M config/default.toml → config/default.toml
@@ -1,6 +1,9 @@ [server] port = 3000 +[site] +title = "Test Site" + [posts] folder = "../posts"
M src/index.js → src/index.js
@@ -17,7 +17,10 @@ const posts = Posts.getFolder(config.posts.folder) app.use( view(`${__dirname}/views`, { - extname: 'njk' + extname: 'njk', + globals: { + site: config.site + } }) )
M src/views/index.njk → src/views/index.njk
@@ -1,5 +1,11 @@+{% extends "layouts/main.njk" %} + +{% block body %} + hello world {% for filename, post in posts %} {{ post.data.get('title') }} {% endfor %} + +{% endblock %}
A src/views/layouts/main.njk
@@ -0,0 +1,13 @@+<!doctype html> +<html lang="en"> + <head> + <meta charset="utf-8"/> + <title>{% block title %}{{ site.title }}{% endblock %}</title> + </head> + <body> + <main> + {% block body %} + {% endblock %} + </main> + </body> +</html>
M src/views/post.njk → src/views/post.njk
@@ -1,3 +1,8 @@+{% extends "layouts/main.njk" %} + +{% block body %} + {{ post.data.title }} {{ post.body | safe }} +{% endblock %}
M src/views/term.njk → src/views/term.njk
@@ -1,3 +1,7 @@+{% extends "layouts/main.njk" %} + +{% block body %} {% for post in posts %} {{ post.data.get('title') }} {% endfor %} +{% endblock %}
M test/index.test.js → test/index.test.js
@@ -11,6 +11,7 @@ test('homepage', t => { return request(app.listen()) .get('/') .expect(200) + .expect(/<title>Test Site<\/title>/) .expect(/hello world/) .expect(/This is a test/) .then(() => t.pass())