Skip to content

Commit

Permalink
优化 helper 为渲染函数
Browse files Browse the repository at this point in the history
  • Loading branch information
zswang committed Sep 30, 2014
1 parent bd1b21f commit 3c1bd06
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ trim_trailing_whitespace = true
[*.md]
trim_trailing_whitespace = false

[*.{html,js,ts,css,scss,xml}]
[*.{html,js,ts,css,scss,xml,jhtmls,json}]
indent_style = space
indent_size = 2

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Web 开发中,大部分的模板都是处理 HTML 格式。针对这一特性
* 格式化输出
* @param {String|Function} template 模板本身 或 模板放在函数行注释中
* @param {Object} data 格式化的数据,默认为空字符串
* @param {Object} helper 附加数据(默认为模板对象)
* @param {Object} helper 附加数据(默认为渲染函数)
* @return {Function|String} 如果只有一个参数则返回渲染函数,否则返回格式化后的字符串
*/
function render(template, data, helper) { ... }
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jhtmls",
"version": "0.0.5",
"version": "0.0.6",
"homepage": "https://github.com/zswang/jhtmls",
"authors": [
"Zswang <[email protected]>"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "jhtmls",
"title": "jhtmls",
"description": "JS and HTML alternate javascript template",
"version": "0.0.5",
"version": "0.0.6",
"homepage": "https://github.com/zswang/jhtmls",
"main": "src/jhtmls.js",
"author": {
Expand Down
9 changes: 7 additions & 2 deletions src/jhtmls.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ void function(exports) {
* 格式化输出
* @param {String|Function} template 模板本身 或 模板放在函数行注释中
* @param {Object} data 格式化的数据,默认为空字符串
* @param {Object} helper 附加数据(默认为模板对象)
* @param {Object} helper 附加数据(默认为渲染函数)
* @return {Function|String} 如果只有一个参数则返回渲染函数,否则返回格式化后的字符串
*/
function render(template, data, helper) {
Expand All @@ -126,8 +126,13 @@ void function(exports) {
* @param{Object} h 辅助对象 helper
*/
var format = function(d, h) {
h = h || exports;
// h = h || fn;
var output = [];
if (typeof h === 'undefined') {
h = function(d) {
fn.call(d, output, encodeHTML, h, exports);
};
}
fn.call(d, output, encodeHTML, h, exports);
return output.join('');
};
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/encode.jhtmls
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<ul>
forEach(function(item) {
<li><em>$item.id</em>city: $item.city, distance: $item.distance, hint: !$item.hint</li>

});
</ul>
18 changes: 18 additions & 0 deletions test/fixtures/recursive.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<ul>
<li>1</li>
<ul>
<li>1.1</li>
<li>1.2</li>
<ul>
<li>1.2.1</li>
<li>1.2.2</li>
</ul></ul> <li>2</li>
<ul>
<li>2.1</li>
<li>2.2</li>
<ul>
<li>2.2.1</li>
<li>2.2.2</li>
<li>2.2.3</li>
</ul> <li>2.3</li>
</ul></ul>
8 changes: 8 additions & 0 deletions test/fixtures/recursive.jhtmls
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<ul>
forEach(function(item) {
<li>#{item.title}</li>
if (item.nodes) {
helper(item.nodes);
}
});
</ul>
43 changes: 43 additions & 0 deletions test/fixtures/recursive.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
[{
"title": "1",
"nodes": [
{
"title": "1.1"
},
{
"title": "1.2",
"nodes": [
{
"title": "1.2.1"
},
{
"title": "1.2.2"
}
]
}
]
}, {
"title": "2",
"nodes": [
{
"title": "2.1"
},
{
"title": "2.2",
"nodes": [
{
"title": "2.2.1"
},
{
"title": "2.2.2"
},
{
"title": "2.2.3"
}
]
},
{
"title": "2.3"
}
]
}]
1 change: 1 addition & 0 deletions test/fixtures/undefined.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<label>zswang--</label>
1 change: 1 addition & 0 deletions test/fixtures/undefined.jhtmls
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<label>#{name}-$title-#{tag}</label>
3 changes: 3 additions & 0 deletions test/fixtures/undefined.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"name": "zswang"
}

0 comments on commit 3c1bd06

Please sign in to comment.