Skip to content

Commit 4f67789

Browse files
author
Philipp Ehrenberg
committed
feat: pass data to partial
1 parent f948177 commit 4f67789

5 files changed

Lines changed: 34 additions & 0 deletions

File tree

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 data = node.attrs.data || 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 (data) {
26+
data = JSON.parse(data);
27+
var result = posthtml()
28+
.use(expressions({ locals: data }))
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 data', function(done) {
47+
test(
48+
`<h1>index</h1><include src="./includes/3.html" data='{"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)