Skip to content

Commit fefdd4f

Browse files
authored
Merge pull request #35 from philicevic/pass-data
feat: pass data to partial
2 parents f948177 + 2670536 commit fefdd4f

6 files changed

Lines changed: 42 additions & 3 deletions

File tree

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ posthtml: (webpack) => {
4545
}
4646
```
4747

48+
### Component options
49+
__locals__: Object containing any local variables that you want to be accessible inside the include component
50+
4851
<h2 align="center">Usage</h2>
4952

5053
__index.html__
@@ -55,14 +58,16 @@ __index.html__
5558
<title>index.html</title>
5659
</head>
5760
<body>
58-
<include src="components/button.html"></include>
61+
<include src="components/button.html" locals='{
62+
"text": "Button"
63+
}'></include>
5964
</body>
6065
</html>
6166
```
6267

6368
__components/button.html__
6469
```html
65-
<button class="button"><div class="button__text">Button</div></button>
70+
<button class="button"><div class="button__text">{{ text }}</div></button>
6671
```
6772

6873
```js
@@ -85,7 +90,7 @@ posthtml([ include({ encoding: 'utf8' }) ])
8590
</head>
8691
<body>
8792
<button class="button">
88-
<div class="button__text">Text</div>
93+
<div class="button__text">Button</div>
8994
</button>
9095
</body>
9196
</html>

index.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ var parser = require('posthtml-parser');
22
var match = require('posthtml/lib/api').match;
33
var fs = require('fs');
44
var path = require('path');
5+
var posthtml = require('posthtml');
6+
var expressions = require('posthtml-expressions');
57

68
module.exports = function(options) {
79
options = options || {};
@@ -13,12 +15,20 @@ module.exports = function(options) {
1315
if (!tree.match) tree.match = match;
1416
tree.match({ tag: 'include' }, function(node) {
1517
var src = node.attrs.src || false;
18+
var locals = node.attrs.locals || false;
1619
var content;
1720
var subtree;
1821
var source;
1922
if (src) {
2023
src = path.resolve(options.root, src);
2124
source = fs.readFileSync(src, options.encoding);
25+
if (locals) {
26+
locals = JSON.parse(locals);
27+
var result = posthtml()
28+
.use(expressions({ locals: locals }))
29+
.process(source, { sync: true });
30+
source = result.html;
31+
}
2232
subtree = tree.parser(source);
2333
subtree.match = tree.match;
2434
subtree.parser = tree.parser;

package-lock.json

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"main": "index.js",
66
"dependencies": {
77
"posthtml": "^0.12.0",
8+
"posthtml-expressions": "^1.1.1",
89
"posthtml-parser": "^0.4.2"
910
},
1011
"devDependencies": {

test/includes/3.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h2>{{ text }}</h2>

test/test.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ describe('Simple test', function() {
4343
);
4444
});
4545

46+
it('include with locals', function(done) {
47+
test(
48+
`<h1>index</h1><include src="./includes/3.html" locals='{"text": 3}'></include>`,
49+
`<h1>index</h1><h2>3</h2>`,
50+
{ root: './test/' },
51+
done
52+
);
53+
});
54+
4655
it('messages dependency for addDependency', function(done) {
4756
var includePath = require('path').resolve('./test/blocks/button/button.html');
4857

0 commit comments

Comments
 (0)