Skip to content

Commit d10ca99

Browse files
authored
Merge pull request #25 from posthtml/milestone-0.4.0
Milestone 0.4.0
2 parents 4be083e + 55ccf0d commit d10ca99

5 files changed

Lines changed: 95 additions & 10 deletions

File tree

README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,55 @@ const options = { root, strict: false };
154154

155155
posthtml([extends(options)]).then(result => console.log(result.html));
156156
```
157+
158+
### slot/fill
159+
160+
Tag names used to match a block with content with a block for inserting content. Default `<block>`
161+
162+
`base.html`
163+
```xml
164+
<html>
165+
<head>
166+
<title><slot name="title"> — Github</slot></title>
167+
</head>
168+
169+
<body>
170+
<div class="content">
171+
<slot name="content"></slot>
172+
</div>
173+
<footer>
174+
<slot name="footer">footer content</slot>
175+
</footer>
176+
</body>
177+
</html>
178+
```
179+
180+
```js
181+
var posthtml = require('posthtml');
182+
var html = `<extends src="base.html">
183+
<fill name="title">How to use posthtml-extend</fill>
184+
<fill name="content">Read the documentation</fill>
185+
</extends>`;
186+
187+
posthtml([require('posthtml-extend')({
188+
slotTagName: 'slot',
189+
fillTagName: 'fill'
190+
})]).process(html).then(function (result) {
191+
console.log(result.html);
192+
});
193+
```
194+
195+
The final HTML will be:
196+
```xml
197+
<html>
198+
<head>
199+
<title>How to use posthtml-extend</title>
200+
</head>
201+
202+
<body>
203+
<div class="content">Read the documentation</div>
204+
<footer>footer content</footer>
205+
</body>
206+
</html>
207+
```
208+

lib/extend.es6

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ export default (options = {}) => {
1616
options.root = options.root || './';
1717
options.plugins = options.plugins || [];
1818
options.strict = Object.prototype.hasOwnProperty.call(options, 'strict') ? !!options.strict : true;
19+
options.slotTagName = options.slotTagName || 'block';
20+
options.fillTagName = options.fillTagName || 'block';
1921

2022
tree = handleExtendsNodes(tree, options, tree.messages);
2123

22-
const blockNodes = getBlockNodes(tree);
24+
const blockNodes = getBlockNodes(tree, options.slotTagName);
2325
for (let blockName of Object.keys(blockNodes)) {
2426
let blockNode = blockNodes[blockName];
2527
blockNode.tag = false;
@@ -42,7 +44,7 @@ function handleExtendsNodes(tree, options, messages) {
4244
let layoutTree = handleExtendsNodes(applyPluginsToTree(parseToPostHtml(layoutHtml), options.plugins), options, messages);
4345

4446
extendsNode.tag = false;
45-
extendsNode.content = mergeExtendsAndLayout(layoutTree, extendsNode, options.strict);
47+
extendsNode.content = mergeExtendsAndLayout(layoutTree, extendsNode, options.strict, options.slotTagName, options.fillTagName);
4648
messages.push({
4749
type: 'dependency',
4850
file: layoutPath,
@@ -60,13 +62,13 @@ function applyPluginsToTree(tree, plugins) {
6062
}
6163

6264

63-
function mergeExtendsAndLayout(layoutTree, extendsNode, strictNames) {
64-
const layoutBlockNodes = getBlockNodes(layoutTree);
65-
const extendsBlockNodes = getBlockNodes(extendsNode.content);
65+
function mergeExtendsAndLayout(layoutTree, extendsNode, strictNames, slotTagName, fillTagName) {
66+
const layoutBlockNodes = getBlockNodes(layoutTree, slotTagName);
67+
const extendsBlockNodes = getBlockNodes(extendsNode.content, fillTagName);
6668

6769
for (let layoutBlockName of Object.keys(layoutBlockNodes)) {
6870
let extendsBlockNode = extendsBlockNodes[layoutBlockName];
69-
if (! extendsBlockNode) {
71+
if (!extendsBlockNode) {
7072
continue;
7173
}
7274

@@ -123,10 +125,10 @@ function getBlockType(blockNode) {
123125
}
124126

125127

126-
function getBlockNodes(content = []) {
128+
function getBlockNodes(content = [], tag) {
127129
let blockNodes = {};
128130

129-
match.call(content, {tag: 'block'}, node => {
131+
match.call(content, {tag}, node => {
130132
if (! node.attrs || ! node.attrs.name) {
131133
throw getError(errors.BLOCK_NO_NAME);
132134
}

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "posthtml-extend",
3-
"version": "0.3.0",
3+
"version": "0.4.0",
44
"description": "Templates extending (Jade-like)",
55
"main": "index.js",
66
"author": "Kirill Maltsev <maltsevkirill@gmail.com>",

test/extend.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,37 @@ describe('Extend', () => {
6060
});
6161
});
6262

63+
it('should extend layout, change <block> to <slot> and <fill>', () => {
64+
mfs.writeFileSync('./layout.html', `
65+
<head><slot name="head">head</slot></head>
66+
<body><slot name="body">body</slot></body>
67+
<sidebar><slot name="sidebar"></slot></sidebar>
68+
<div><slot name="ad">ad</slot></div>
69+
<footer><slot name="footer">footer</slot></footer>
70+
`);
71+
72+
const options = {
73+
slotTagName: 'slot',
74+
fillTagName: 'fill',
75+
};
76+
77+
return init(`
78+
<extends src="layout.html">
79+
<fill name="ad"></fill>
80+
<fill name="head"><title>hello world!</title></fill>
81+
<fill name="body">Some body content</fill>
82+
</extends>
83+
`, options).then(html => {
84+
expect(html).toBe(cleanHtml(`
85+
<head><title>hello world!</title></head>
86+
<body>Some body content</body>
87+
<sidebar></sidebar>
88+
<div></div>
89+
<footer>footer</footer>
90+
`));
91+
});
92+
});
93+
6394
it('should extend layout with plugin', () => {
6495
mfs.writeFileSync('./layout.html', `
6596
<block name="content"></block>

0 commit comments

Comments
 (0)