Skip to content

Commit

Permalink
support json as include's second parameter.Fixes #57
Browse files Browse the repository at this point in the history
  • Loading branch information
yiminghe committed Mar 26, 2015
1 parent 30b48e8 commit eefea21
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 30 deletions.
17 changes: 17 additions & 0 deletions examples/simple.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8"/>
<meta name="viewport"
content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-touch-fullscreen" content="yes">
<script src="/node_modules/modulex/build/modulex-debug.js?nowrap"></script>
<title> simple demo </title>
</head>
<body>
<script>
require('./simple');
</script>
</body>
</html>
5 changes: 0 additions & 5 deletions examples/index.md → examples/simple.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
# Simple Usage
---

````js
var XTemplate = require('../');
console.log(new XTemplate('Hello {{world}}!').render({
world: 'world'
}));
````
4 changes: 2 additions & 2 deletions lib/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,8 @@ function generateFunction(self, func, block, escape) {
var isModule = self.config.isModule;

if (idString === 'include' || idString === 'parse' || idString === 'extend') {
if (!func.params || func.params.length !== 1) {
throw new Error('include/parse/extend can only has one parameter!');
if (!func.params || func.params.length > 2) {
throw new Error('include/parse/extend can only has at most two parameter!');
}
}

Expand Down
37 changes: 22 additions & 15 deletions lib/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,26 @@ function renderTpl(tpl) {
}
}

function getIncludeScope(scope, option) {
var params = option.params;
var newScope = scope;
var newScopeData = params[1];
var hash = option.hash;
if (hash) {
if (newScopeData) {
newScopeData = util.mix({}, newScopeData);
} else {
newScopeData = {};
}
util.mix(newScopeData, hash);
}
// sub template scope
if (newScopeData) {
newScope = new Scope(newScopeData, undefined, scope);
}
return newScope;
}

XTemplateRuntime.prototype = {
constructor: XTemplateRuntime,

Expand Down Expand Up @@ -287,33 +307,20 @@ XTemplateRuntime.prototype = {

include: function (scope, option, buffer, tpl) {
var params = option.params;
var newScope;
newScope = scope;
var hash = option.hash;
var escape = option && option.escape;
// sub template scope
if (hash) {
newScope = new Scope(hash, undefined, scope);
}
if (!params[0]) {
return buffer.error('include command required a non-empty parameter');
}
buffer = includeInternal(this, newScope, escape, buffer, tpl, params[0]);
buffer = includeInternal(this, getIncludeScope(scope, option), escape, buffer, tpl, params[0]);
return buffer;
},

includeModule: function (scope, option, buffer, tpl) {
var params = option.params;
var newScope = scope;
var hash = option.hash;
// sub template scope
if (hash) {
newScope = new Scope(hash, undefined, scope);
}
if (!params[0]) {
return buffer.error('include command required a non-empty parameter');
}
buffer = includeModuleInternal(this, newScope, buffer, tpl, params[0]);
buffer = includeModuleInternal(this, getIncludeScope(scope, option), buffer, tpl, params[0]);
return buffer;
},

Expand Down
8 changes: 5 additions & 3 deletions lib/runtime/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,10 @@ module.exports = util = {
},

mix: function (t, s) {
for (var p in s) {
t[p] = s[p];
if (s) {
for (var p in s) {
t[p] = s[p];
}
}
return t;
},
Expand Down Expand Up @@ -130,4 +132,4 @@ module.exports = util = {
}
return ret;
}
};
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "xtemplate",
"version": "4.1.3",
"version": "4.2.0",
"author": "yiminghe <[email protected]>",
"engines": {
"node": ">=0.10"
Expand Down
6 changes: 3 additions & 3 deletions tests/browser/specs/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,13 +216,13 @@ describe('error detection', function () {
it('error when include without parameter', function () {
expect(function () {
new XTemplate("{{include()}}").render();
}).to.throwException(/include\/parse\/extend can only has one parameter!/);
}).to.throwException(/include\/parse\/extend can only has at most two parameter!/);
});

it('error when include more than one parameter', function () {
expect(function () {
new XTemplate("{{include(a, b)}}").render();
}).to.throwException(/include\/parse\/extend can only has one parameter!/);
new XTemplate("{{include(a, b,c)}}").render();
}).to.throwException(/include\/parse\/extend can only has at most two parameter!/);
});

it('error when include empty parameter', function () {
Expand Down
13 changes: 12 additions & 1 deletion tests/browser/specs/sub-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('sub template', function () {
expect(tpl.name).to.equal('xtemplate-test/' + tplName);
expect(tpl.originalName).to.equal('./' + tplName);
expect(tpl.parent.name).to.equal('xtemplate-test/' + tplName2);
(require.async||require)([name],
(require.async || require)([name],
function (content) {
if (typeof content === 'string') {
try {
Expand Down Expand Up @@ -153,4 +153,15 @@ describe('sub template', function () {
}).render({title: 1});
expect(ret).to.be('1');
});

it('will support json as second parameter', function () {
var tplName = uuid.v4();
var tplName2 = uuid.v4();
define(tplName, '{{include("' + tplName2 + '",{title:3,title2:2},title=1)}}');
define(tplName2, '{{title}}{{title2}}');
var ret = new XTemplate({
name: tplName
}).render({});
expect(ret).to.be('12');
});
});

1 comment on commit eefea21

@dead-horse
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💯

Please sign in to comment.