config/templates-dir
Note
You need version
1.4.0or newer.
Previously, only one template was supported. However, you can now have multiple. This can be great to, for example, give the index page a slightly different layout.
You can choose which template to use via the frontmatter. First though, we need to define which directory we are going to store our templates in. Let's take the following folder structure:
.
├── content/
│ ├── index.md
│ └── favourite_recipe.md
├── templates/
│ ├── index.ume.html
│ └── template.ume.html
└── index.js
Our index.js will look like this:
// --- Import Express and umejs ---
const ume = require('@dantenl/umejs');
const express = require('express');
const path = require('path');
// --- Initialise Express ---
const app = express();
const PORT = 3000;
// --- Initialise umejs ---
const umeConfig = ume({
// let's define a default:
templatePath: path.join(__dirname, 'templates', 'template.ume.html'),
// let's define our templates directory
templatesDir: path.join(__dirname, 'templates'),
contentDir: path.join(__dirname, 'content'),
});
// --- Set up the umejs middleware ---
app.use('/blog/{*slug}', umeConfig);
// --- Get the server running ---
const server = app.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}`);
});
// --- Fully close umejs ---
process.on('SIGINT', async () => {
server.close();
await umeConfig.close();
process.exit(0);
});
If we leave it at that, our index.md and favourite_recipe.md will both just use the same template.ume.html. For more details on what a template looks like, please see config/templatePath.
However, let's change the template for our index.md! This can be done quite easily:
<!-- /content/index.md -->
---
_TEMPLATE: "index"
# the file extension (either .ume.html or just .html) will be automatically appended by umejs
---
# welcome to my blog!
When umejs builds this file, it now knows that it shouldn't use the theme-provided template or your provided default template, but rather our index.ume.html!
Tip
It's also possible to reference a template from a theme! For example, if a theme provides a seperate template for the index page, you can simply set your
_TEMPLATEkey in the frontmatter toindex.NOTE: if you then create your own
index.ume.htmlinside the templates directory, you will override the one provided by the theme!