Skip to content

Commit dff3e14

Browse files
Add virtual templates
1 parent 0b549c2 commit dff3e14

17 files changed

+65
-18
lines changed

docs/get-started.md

-2
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ export default function(eleventyConfig) {
5252
dir: {
5353
// The folder where all your content will live:
5454
input: 'app',
55-
// Use layouts from the plugin
56-
layouts: '../node_modules/@x-govuk/govuk-eleventy-plugin/layouts'
5755
}
5856
}
5957
};

eleventy.config.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,7 @@ export default function (eleventyConfig) {
9090
htmlTemplateEngine: 'njk',
9191
markdownTemplateEngine: 'njk',
9292
dir: {
93-
input: 'docs',
94-
layouts: '../layouts'
93+
input: 'docs'
9594
},
9695
pathPrefix: process.env.GITHUB_ACTIONS && '/govuk-eleventy-plugin/'
9796
}

index.js

+8-1
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ import { generateAssets } from './lib/events/generate-govuk-assets.js'
88
import { md } from './lib/markdown-it.js'
99
import { nunjucksConfig } from './lib/nunjucks.js'
1010
import { scssExtension } from './lib/extensions/scss.js'
11+
import { getTemplates } from './lib/utils.js'
1112

12-
export default function (eleventyConfig, pluginOptions = {}) {
13+
export default async function (eleventyConfig, pluginOptions = {}) {
1314
const { pathPrefix } = eleventyConfig
1415

1516
// Plugin options
@@ -23,6 +24,12 @@ export default function (eleventyConfig, pluginOptions = {}) {
2324
eleventyConfig.addExtension('scss', scssExtension)
2425
eleventyConfig.addTemplateFormats('scss')
2526

27+
// Virtual templates
28+
const templates = await getTemplates(eleventyConfig)
29+
for (const [virtualPath, template] of Object.entries(templates)) {
30+
eleventyConfig.addTemplate(virtualPath, template)
31+
}
32+
2633
// Collections
2734
for (const [name, collection] of Object.entries(collections)) {
2835
eleventyConfig.addCollection(name, collection)

lib/nunjucks.js

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export function nunjucksConfig(eleventyConfig) {
2525

2626
const searchPaths = [
2727
'./node_modules/@x-govuk/govuk-eleventy-plugin',
28+
'./node_modules/@x-govuk/govuk-eleventy-plugin/lib',
2829
'./node_modules/govuk-frontend/dist',
2930
resolveNpmModule('@x-govuk/govuk-prototype-components'),
3031
...(includes ? [path.join(input, includes)] : []),
File renamed without changes.

layouts/collection.njk lib/templates/collection.njk

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{% extends "layouts/base.njk" %}
1+
{% extends "templates/base.njk" %}
22

33
{% block main %}
44
{{ xGovukMasthead({
@@ -47,7 +47,7 @@
4747

4848
{% if aside or related %}
4949
<div class="govuk-grid-column-one-third-from-desktop">
50-
{% include "layouts/shared/related.njk" %}
50+
{% include "templates/shared/related.njk" %}
5151
</div>
5252
{% endif %}
5353
</div>
File renamed without changes.

layouts/page.njk lib/templates/page.njk

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{% extends "layouts/base.njk" %}
1+
{% extends "templates/base.njk" %}
22

33
{% block beforeContent %}
44
{{ govukBreadcrumbs({
@@ -15,5 +15,5 @@
1515

1616
{{ appProseScope(content) if content }}
1717

18-
{% include "layouts/shared/related.njk" %}
18+
{% include "templates/shared/related.njk" %}
1919
{% endblock %}

layouts/post.njk lib/templates/post.njk

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{% extends "layouts/base.njk" %}
1+
{% extends "templates/base.njk" %}
22

33
{% block beforeContent %}
44
{{ govukBreadcrumbs({
@@ -37,7 +37,7 @@
3737

3838
{% if aside or related %}
3939
<div class="govuk-grid-column-one-third-from-desktop">
40-
{% include "layouts/shared/related.njk" %}
40+
{% include "templates/shared/related.njk" %}
4141
</div>
4242
{% endif %}
4343
</div>

layouts/product.njk lib/templates/product.njk

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{% extends "layouts/base.njk" %}
1+
{% extends "templates/base.njk" %}
22

33
{% block main %}
44
<main id="main-content" role="main" {%- if mainLang %} lang="{{ mainLang }}"{% endif %}>
@@ -27,7 +27,7 @@
2727
{% block content %}
2828
{{ appProseScope(content) if content }}
2929

30-
{% include "layouts/shared/related.njk" %}
30+
{% include "templates/shared/related.njk" %}
3131
{% endblock %}
3232
</div>
3333
</div>
File renamed without changes.
File renamed without changes.

layouts/sitemap.njk lib/templates/sitemap.njk

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{% extends "layouts/base.njk" %}
1+
{% extends "templates/base.njk" %}
22

33
{# Create breadcrumbs manually as sitemap should be excluded from collections #}
44
{% block beforeContent %}

layouts/sub-navigation.njk lib/templates/sub-navigation.njk

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{% extends "layouts/base.njk" %}
1+
{% extends "templates/base.njk" %}
22

33
{% block beforeContent %}
44
{{ govukBreadcrumbs({
@@ -22,7 +22,7 @@
2222

2323
{{ appProseScope(content) if content }}
2424

25-
{% include "layouts/shared/related.njk" %}
25+
{% include "templates/shared/related.njk" %}
2626
</div>
2727
</div>
2828
{% endblock %}

layouts/tag.njk lib/templates/tag.njk

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{% extends "layouts/base.njk" %}
1+
{% extends "templates/base.njk" %}
22

33
{% block beforeContent %}
44
{{ govukBreadcrumbs({

layouts/tags.njk lib/templates/tags.njk

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{% extends "layouts/base.njk" %}
1+
{% extends "templates/base.njk" %}
22

33
{% block beforeContent %}
44
{{ govukBreadcrumbs({

lib/utils.js

+42
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
import path from 'node:path'
22
import fs from 'node:fs/promises'
33

4+
/**
5+
* Get file contents
6+
* @param {string} filePath - Path to file
7+
* @returns {string} File contents
8+
*/
9+
export async function getFileContents(filePath) {
10+
filePath = path.join(import.meta.dirname, '..', filePath)
11+
12+
return await fs.readFile(filePath, { encoding: 'utf8' })
13+
}
14+
415
/**
516
* Read contents of SCSS settings file
617
* @param {object} dir - Eleventy directories
@@ -27,6 +38,37 @@ export async function getScssSettings(dir, options) {
2738
return settings
2839
}
2940

41+
/**
42+
* Get virtual templates
43+
* @param {object} eleventyConfig - Eleventy config
44+
* @returns {object} Template names and strings
45+
*/
46+
export async function getTemplates(eleventyConfig) {
47+
const { includes, layouts } = eleventyConfig.dir
48+
const layoutDir = layouts || includes
49+
const layoutNames = [
50+
'base',
51+
'collection',
52+
'feed',
53+
'page',
54+
'post',
55+
'product',
56+
'search-index',
57+
'sitemap',
58+
'sub-navigation',
59+
'tag',
60+
'tags'
61+
]
62+
const templates = {}
63+
64+
for (const name of layoutNames) {
65+
const templateString = await getFileContents(`lib/templates/${name}.njk`)
66+
templates[`${layoutDir}/${name}.njk`] = templateString
67+
}
68+
69+
return templates
70+
}
71+
3072
/**
3173
* Normalise value provided to a filter. Checks that a given value exists
3274
* before performing a transformation.

0 commit comments

Comments
 (0)