all repos — archive/homestead @ 7455233d74ffe056c4a5927c9272516fdc0e5dfc

My future indieweb platform

feat: make templates extend common layout
Alan Pearce alan@alanpearce.eu
Sun, 18 Jun 2017 22:12:48 +0200
commit

7455233d74ffe056c4a5927c9272516fdc0e5dfc

parent

4ce0f287b64d2d55c9bdd31dee418ba405762273

M config/default.tomlconfig/default.toml
@@ -1,6 +1,9 @@ [server]
 port = 3000
 
+[site]
+title = "Test Site"
+
 [posts]
 folder = "../posts"
 
M src/index.jssrc/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.njksrc/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.njksrc/views/post.njk
@@ -1,3 +1,8 @@+{% extends "layouts/main.njk" %}
+
+{% block body %}
+
 {{ post.data.title }}
 
 {{ post.body | safe }}
+{% endblock %}
M src/views/term.njksrc/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.jstest/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())