diff --git a/lib/render.js b/lib/render.js index 082e58b..735a4d0 100644 --- a/lib/render.js +++ b/lib/render.js @@ -50,9 +50,14 @@ function render(file, enc, cb) { // Add this page's front matter pageData = extend(pageData, page.attributes); + // Full path to page excluding filename + var pagePath = path.relative(this.options.root, path.dirname(file.path)); + if (pagePath) pagePath += '/'; + // Finish by adding constants pageData = extend(pageData, { page: path.basename(file.path, path.extname(file.path)), + path: pagePath, layout: layout, root: processRoot(file.path, this.options.root) }); diff --git a/readme.md b/readme.md index 83dd9b8..82b8101 100644 --- a/readme.md +++ b/readme.md @@ -135,6 +135,8 @@ Data can also be a `.js` file with a `module.exports`. The data returned by the Data can also be inserted into the page itself with a Front Matter template at the top of the file. +The reserved `path` variable is added to every page. This stores the path to the current page, relative to the root and without the filename. + Lastly, the reserved `page` variable is added to every page template as it renders. It contains the name of the page being rendered, without the extension. ## CLI diff --git a/test/fixtures/variable-path/build/index.html b/test/fixtures/variable-path/build/index.html new file mode 100644 index 0000000..169fc68 --- /dev/null +++ b/test/fixtures/variable-path/build/index.html @@ -0,0 +1,7 @@ + +
+ + + + + diff --git a/test/fixtures/variable-path/build/subfolder/another/index.html b/test/fixtures/variable-path/build/subfolder/another/index.html new file mode 100644 index 0000000..89b4f50 --- /dev/null +++ b/test/fixtures/variable-path/build/subfolder/another/index.html @@ -0,0 +1,7 @@ + + + subfolder/another/ +subfolder/another/
+ + + diff --git a/test/fixtures/variable-path/build/subfolder/index.html b/test/fixtures/variable-path/build/subfolder/index.html new file mode 100644 index 0000000..83dac37 --- /dev/null +++ b/test/fixtures/variable-path/build/subfolder/index.html @@ -0,0 +1,7 @@ + + + subfolder/ +subfolder/
+ + + diff --git a/test/fixtures/variable-path/expected/index.html b/test/fixtures/variable-path/expected/index.html new file mode 100644 index 0000000..169fc68 --- /dev/null +++ b/test/fixtures/variable-path/expected/index.html @@ -0,0 +1,7 @@ + + + + + + + diff --git a/test/fixtures/variable-path/expected/subfolder/another/index.html b/test/fixtures/variable-path/expected/subfolder/another/index.html new file mode 100644 index 0000000..89b4f50 --- /dev/null +++ b/test/fixtures/variable-path/expected/subfolder/another/index.html @@ -0,0 +1,7 @@ + + + subfolder/another/ +subfolder/another/
+ + + diff --git a/test/fixtures/variable-path/expected/subfolder/index.html b/test/fixtures/variable-path/expected/subfolder/index.html new file mode 100644 index 0000000..83dac37 --- /dev/null +++ b/test/fixtures/variable-path/expected/subfolder/index.html @@ -0,0 +1,7 @@ + + + subfolder/ +subfolder/
+ + + diff --git a/test/fixtures/variable-path/layouts/default.html b/test/fixtures/variable-path/layouts/default.html new file mode 100644 index 0000000..1ac4174 --- /dev/null +++ b/test/fixtures/variable-path/layouts/default.html @@ -0,0 +1,5 @@ + + + {{> body}} + + diff --git a/test/fixtures/variable-path/pages/index.html b/test/fixtures/variable-path/pages/index.html new file mode 100644 index 0000000..2c741f9 --- /dev/null +++ b/test/fixtures/variable-path/pages/index.html @@ -0,0 +1,2 @@ +{{path}} +{{> partial}} diff --git a/test/fixtures/variable-path/pages/subfolder/another/index.html b/test/fixtures/variable-path/pages/subfolder/another/index.html new file mode 100644 index 0000000..2c741f9 --- /dev/null +++ b/test/fixtures/variable-path/pages/subfolder/another/index.html @@ -0,0 +1,2 @@ +{{path}} +{{> partial}} diff --git a/test/fixtures/variable-path/pages/subfolder/index.html b/test/fixtures/variable-path/pages/subfolder/index.html new file mode 100644 index 0000000..2c741f9 --- /dev/null +++ b/test/fixtures/variable-path/pages/subfolder/index.html @@ -0,0 +1,2 @@ +{{path}} +{{> partial}} diff --git a/test/fixtures/variable-path/partials/partial.html b/test/fixtures/variable-path/partials/partial.html new file mode 100644 index 0000000..5c69979 --- /dev/null +++ b/test/fixtures/variable-path/partials/partial.html @@ -0,0 +1 @@ +{{path}}
diff --git a/test/test.js b/test/test.js index 8506373..89dddb5 100644 --- a/test/test.js +++ b/test/test.js @@ -205,6 +205,24 @@ describe('Panini variables', () => { }); }); + it('{{path}} variable that stores the path to the current page, excluding filename', done => { + var p = new Panini({ + root: FIXTURES + 'variable-path/pages/', + layouts: FIXTURES + 'variable-path/layouts/', + partials: FIXTURES + 'variable-path/partials/' + }); + + p.refresh(); + + src(FIXTURES + 'variable-path/pages/**/*.html') + .pipe(p.render()) + .pipe(dest(FIXTURES + 'variable-path/build')) + .on('finish', () => { + equal(FIXTURES + 'variable-path/expected', FIXTURES + 'variable-path/build'); + done(); + }); + }); + it('{{layout}} variable that stores the current layout', done => { var p = new Panini({ root: FIXTURES + 'variable-layout/pages/',