Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Page path variable #232

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
});
Expand Down
2 changes: 2 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions test/fixtures/variable-path/build/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html>
<body>

<p></p>

</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html>
<body>
subfolder/another/
<p>subfolder/another/</p>

</body>
</html>
7 changes: 7 additions & 0 deletions test/fixtures/variable-path/build/subfolder/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html>
<body>
subfolder/
<p>subfolder/</p>

</body>
</html>
7 changes: 7 additions & 0 deletions test/fixtures/variable-path/expected/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html>
<body>

<p></p>

</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html>
<body>
subfolder/another/
<p>subfolder/another/</p>

</body>
</html>
7 changes: 7 additions & 0 deletions test/fixtures/variable-path/expected/subfolder/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html>
<body>
subfolder/
<p>subfolder/</p>

</body>
</html>
5 changes: 5 additions & 0 deletions test/fixtures/variable-path/layouts/default.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<html>
<body>
{{> body}}
</body>
</html>
2 changes: 2 additions & 0 deletions test/fixtures/variable-path/pages/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{{path}}
{{> partial}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{{path}}
{{> partial}}
2 changes: 2 additions & 0 deletions test/fixtures/variable-path/pages/subfolder/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{{path}}
{{> partial}}
1 change: 1 addition & 0 deletions test/fixtures/variable-path/partials/partial.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>{{path}}</p>
18 changes: 18 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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/',
Expand Down