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 inline partials to be used within layout #285

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
2 changes: 2 additions & 0 deletions bin/panini.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var options = {
"root": String,
"layouts": String,
"partials": String,
"inlinelayout": String,
"data": String,
"helpers": String,
"output": String,
Expand All @@ -23,6 +24,7 @@ var shorthands = {
"r": "--root",
"l": "--layouts",
"p": "--partials",
"i": "--inlinelayout",
"d": "--data",
"h": "--helpers",
"o": "--output",
Expand Down
1 change: 1 addition & 0 deletions lib/helpMessage.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module.exports = function() {
' --root (required) path to the root folder all pages live in\n' +
' --output (required) path to the folder compiled pages should get sent to\n' +
' --partials path to root folder for partials \n' +
' --inlinelayout prefix of inline partial to look for \n' +
' --helpers path to folder for additional helpers \n' +
' --data path to folder for additional data \n' +
'\n' +
Expand Down
30 changes: 30 additions & 0 deletions lib/render.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,36 @@ function render(file, enc, cb) {
}
}

// Determine which inline partial layout prefix to use
var inlineLayout = typeof page.attributes.inlinelayout !== "undefined" ? page.attributes.inlinelayout : typeof this.options.inlinelayout !== "undefined" ? this.options.inlinelayout : 'layout-';
//remove Handlebars.partials starting with inlineLayout prefix
var inlineLayoutPartialList = Object.keys(this.Handlebars.partials).filter(
function (partial) {
return (partial.indexOf(inlineLayout) === 0);
}
);
inlineLayoutPartialList.forEach(partial => delete this.Handlebars.partials[partial]);
//add to Handlebars.partials any inline partials starting with "layout-"
if (page.body.indexOf('{{#*inline') !== -1 || page.body.indexOf('{{~#*inline') !== -1) {
// var inlinePartialAndCommentRegex = /{{!--(?<comment>[.\s\S]*?)--}}|(?<partial>{{#\*inline[\s]+\"(?<name>layout-[0-9a-zA-Z_-]*)\"}}(?<body>[.\s\S]*?){{\/inline}}[\s]*)/gm;
var inlinePartialAndCommentRegex = new RegExp(String.raw`{{!--(?<comment>[.\s\S]*?)--}}|(?<partial>{{#\*inline[\s]+\"(?<name>${inlineLayout}[0-9a-zA-Z_-]*)\"}}(?<body>[.\s\S]*?){{\/inline}}[\s]*)`, "gm");
var replaceWith = '';
var bodyText = page.body;
var inlinePartialMatch;
while (inlinePartialMatch = inlinePartialAndCommentRegex.exec(page.body)) {
// Add inline partials from page.body starting with "layout-"
if (typeof inlinePartialMatch.groups.partial !== 'undefined') {//hasOwnProperty not working?
this.Handlebars.registerPartial(
inlinePartialMatch.groups.name,
this.Handlebars.compile(inlinePartialMatch.groups.body + '\n')
);
bodyText = bodyText.replace(inlinePartialMatch.groups.partial, replaceWith);
}
}
page.body = bodyText;//amended body - removed inline partials starting with "layout-"
//note: would not harm to use body with removed handlebars comments
}

// Now create Handlebars templates out of them
var pageTemplate = this.Handlebars.compile(page.body + '\n');

Expand Down
53 changes: 53 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,59 @@ Path to a folder containing HTML partials. Partial files can have the extension
{{> header}}
```

### `inlinelayout`

**Type:** `String`

Inline partial name prefix, if not set - the default prefix will be `layout-`. Page Inline Partials to be used within layout pages.

```html
{{#*inline "layout-inline-partial-bot"}}
<!-- Replace Header -->
{{/inline}}
{{#*inline "layout-inline-partial-top"}}
<!-- Replace Footer -->
{{/inline}}
<!-- Body Content -->
```

The page inline partials with `inlinelayout` prefix can be used within the layouts. If there is not a corresponding inline partial on the page, then the default content will be displayed. Note: the default content can be made empty.

```html
{{#> layout-inline-partial-top}}
<!-- Default Header up here -->
{{/layout-inline-partial-top}}
{{> body}}
{{#> layout-inline-partial-bot}}
<!-- Footer down here -->
{{/layout-inline-partial-bot}}
```

To use an `inlinelayout` other than the default `layout-` prefix or `panini.options.inlinelayout` prefix on a specific page, override it in the Front Matter on that page.

```html
---
inlinelayout: alt-inlinelayout-
---
{{#*inline "alt-inlinelayout-inline-partial-bot"}}
<!-- Replace Header -->
{{/inline}}
{{#*inline "alt-inlinelayout-inline-partial-top"}}
<!-- Replace Footer -->
{{/inline}}
<!-- Body Content -->
```
layout
```html
{{#> alt-inlinelayout-inline-partial-top}}
<!-- Default Header up here -->
{{/alt-inlinelayout-inline-partial-top}}
{{> body}}
{{#> alt-inlinelayout-inline-partial-bot}}
<!-- Footer down here -->
{{/alt-inlinelayout-inline-partial-bot}}
```

### `helpers`

**Type:** `String`
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<html>
<body>

<p>layout-inline-partial-top content</p>

<p>Page Body</p>

<p>layout-inline-partial-bot content</p>

</body>
</html>
11 changes: 11 additions & 0 deletions test/fixtures/partials-inline/build/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<html>
<body>

<p>layout-inline-partial-top content</p>

<p>Page Body</p>

<p>layout-inline-partial-bot content</p>

</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<html>
<body>
<p>No layout-inline-partial-top default content</p>
<p>Page Body</p>

<p>layout-inline-partial-bot content - commented</p>

</body>
</html>
9 changes: 9 additions & 0 deletions test/fixtures/partials-inline/build/inline-partial-bot.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<html>
<body>
<p>No layout-inline-partial-top default content</p>
<p>Page Body</p>

<p>layout-inline-partial-bot content</p>

</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html>
<body>
<p>No layout-inline-partial-top default content</p>
<p>Page Body</p>
<p>No layout-inline-partial-bot default content</p>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<html>
<body>

<p>layout-inline-partial-top content</p>

<p>Page Body</p>
<p>No layout-inline-partial-bot default content</p>
</body>
</html>
9 changes: 9 additions & 0 deletions test/fixtures/partials-inline/build/inline-partial-top.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<html>
<body>

<p>layout-inline-partial-top content</p>

<p>Page Body</p>
<p>No layout-inline-partial-bot default content</p>
</body>
</html>
7 changes: 7 additions & 0 deletions test/fixtures/partials-inline/build/no-inline-partial.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html>
<body>
<p>No layout-inline-partial-top default content</p>
<p>Page Body</p>
<p>No layout-inline-partial-bot default content</p>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<html>
<body>

<p>layout-inline-partial-top content</p>

<p>Page Body</p>

<p>layout-inline-partial-bot content</p>

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

<p>layout-inline-partial-top content</p>

<p>Page Body</p>

<p>layout-inline-partial-bot content</p>

</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<html>
<body>
<p>No layout-inline-partial-top default content</p>
<p>Page Body</p>

<p>layout-inline-partial-bot content - commented</p>

</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<html>
<body>
<p>No layout-inline-partial-top default content</p>
<p>Page Body</p>

<p>layout-inline-partial-bot content</p>

</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html>
<body>
<p>No layout-inline-partial-top default content</p>
<p>Page Body</p>
<p>No layout-inline-partial-bot default content</p>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<html>
<body>

<p>layout-inline-partial-top content</p>

<p>Page Body</p>
<p>No layout-inline-partial-bot default content</p>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<html>
<body>

<p>layout-inline-partial-top content</p>

<p>Page Body</p>
<p>No layout-inline-partial-bot default content</p>
</body>
</html>
7 changes: 7 additions & 0 deletions test/fixtures/partials-inline/expected/no-inline-partial.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<html>
<body>
<p>No layout-inline-partial-top default content</p>
<p>Page Body</p>
<p>No layout-inline-partial-bot default content</p>
</body>
</html>
11 changes: 11 additions & 0 deletions test/fixtures/partials-inline/layouts/default.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<html>
<body>
{{#> layout-inline-partial-top}}
<p>No layout-inline-partial-top default content</p>
{{/layout-inline-partial-top}}
{{> body}}
{{#> layout-inline-partial-bot}}
<p>No layout-inline-partial-bot default content</p>
{{/layout-inline-partial-bot}}
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{{!--
{{#*inline "layout-inline-partial-bot"}}
<p>layout-inline-partial-bot content in comment</p>
{{/inline}}
{{#*inline "layout-inline-partial-top"}}
<p>layout-inline-partial-top content in comment</p>
{{/inline}}
--}}
{{#*inline "layout-inline-partial-bot"}}
<p>layout-inline-partial-bot content</p>
{{/inline}}
{{#*inline "layout-inline-partial-top"}}
<p>layout-inline-partial-top content</p>
{{/inline}}
<p>Page Body</p>
{{!--
{{#*inline "layout-inline-partial-bot"}}
<p>layout-inline-partial-bot content in comment bottom page</p>
{{/inline}}
{{#*inline "layout-inline-partial-top"}}
<p>layout-inline-partial-top content in comment bottom page</p>
{{/inline}}
--}}
7 changes: 7 additions & 0 deletions test/fixtures/partials-inline/pages/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{{#*inline "layout-inline-partial-bot"}}
<p>layout-inline-partial-bot content</p>
{{/inline}}
{{#*inline "layout-inline-partial-top"}}
<p>layout-inline-partial-top content</p>
{{/inline}}
<p>Page Body</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{{!--
{{#*inline "layout-inline-partial-top"}}
<p>layout-inline-partial-top content</p>
{{/inline}}
--}}
{{#*inline "layout-inline-partial-bot"}}
<p>layout-inline-partial-bot content - commented</p>
{{/inline}}
<p>Page Body</p>
4 changes: 4 additions & 0 deletions test/fixtures/partials-inline/pages/inline-partial-bot.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{{#*inline "layout-inline-partial-bot"}}
<p>layout-inline-partial-bot content</p>
{{/inline}}
<p>Page Body</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{{!--
{{#*inline "layout-inline-partial-bot"}}
<p>layout-inline-partial-bot content in comment</p>
{{/inline}}
{{#*inline "layout-inline-partial-top"}}
<p>layout-inline-partial-top content in comment</p>
{{/inline}}
--}}
<p>Page Body</p>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{{#*inline "layout-inline-partial-top"}}
<p>layout-inline-partial-top content</p>
{{/inline}}
{{!--
{{#*inline "layout-inline-partial-bot"}}
<p>layout-inline-partial-bot content - commented</p>
{{/inline}}
--}}
<p>Page Body</p>
4 changes: 4 additions & 0 deletions test/fixtures/partials-inline/pages/inline-partial-top.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{{#*inline "layout-inline-partial-top"}}
<p>layout-inline-partial-top content</p>
{{/inline}}
<p>Page Body</p>
1 change: 1 addition & 0 deletions test/fixtures/partials-inline/pages/no-inline-partial.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>Page Body</p>
18 changes: 18 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,24 @@ describe('Panini', () => {
});
});

it('builds a page with inline partials used within layout', done => {
var p = new Panini({
root: FIXTURES + 'partials-inline/pages/',
layouts: FIXTURES + 'partials-inline/layouts/',
inlinelayout: 'layout-'
});

p.refresh();

src(FIXTURES + 'partials-inline/pages/*')
.pipe(p.render())
.pipe(dest(FIXTURES + 'partials-inline/build'))
.on('finish', () => {
equal(FIXTURES + 'partials-inline/expected', FIXTURES + 'partials-inline/build');
done();
});
});

it('builds a page with custom data', done => {
var p = new Panini({
root: FIXTURES + 'data-page/pages/',
Expand Down