├── .gitignore ├── static ├── images │ ├── noise.png │ ├── favicon.png │ ├── handlebars-mark.png │ ├── handlebars_logo.png │ ├── handlebars-devswag.png │ └── forkme_right_darkblue_121621.png ├── fonts │ ├── glyphicons-halflings-regular.eot │ ├── glyphicons-halflings-regular.ttf │ ├── glyphicons-halflings-regular.woff │ └── glyphicons-halflings-regular.woff2 ├── js │ ├── demo.js │ └── handlebars.runtime-v3.0.1.js ├── stylesheets │ ├── sunburst.css │ └── application.css └── css │ └── demo.css ├── s.js ├── posts ├── reference.md ├── block_helpers.md ├── builtin_helpers.md ├── precompilation.md ├── demo │ ├── HTML-escapes.md │ ├── demo.md │ ├── expressions-subexpressions.md │ ├── execution-1.md │ ├── expressions-whitespaccontrol-2.md │ ├── expressions-whitespaccontrol-1.md │ ├── SafeString.md │ ├── expressions-1.md │ ├── expressions-2.md │ ├── expressions-3.md │ ├── SafeString-escapeExpression.md │ └── property-not-a-valid-identifier.md ├── execution.md ├── expressions.md └── index.md ├── package.json ├── README.md ├── _template ├── demo.html └── default.html ├── reference.html ├── block_helpers.html ├── precompilation.html ├── builtin_helpers.html ├── demo ├── HTML-escapes.html ├── demo.html ├── expressions-subexpressions.html ├── execution-1.html ├── expressions-1.html ├── SafeString.html ├── expressions-whitespaccontrol-2.html ├── expressions-whitespaccontrol-1.html ├── expressions-2.html ├── expressions-3.html ├── SafeString-escapeExpression.html └── property-not-a-valid-identifier.html ├── execution.html ├── README.html ├── gulpfile.js ├── expressions.html └── index.html /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules -------------------------------------------------------------------------------- /static/images/noise.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onface/handlebars-cn/HEAD/static/images/noise.png -------------------------------------------------------------------------------- /static/images/favicon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onface/handlebars-cn/HEAD/static/images/favicon.png -------------------------------------------------------------------------------- /static/images/handlebars-mark.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onface/handlebars-cn/HEAD/static/images/handlebars-mark.png -------------------------------------------------------------------------------- /static/images/handlebars_logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onface/handlebars-cn/HEAD/static/images/handlebars_logo.png -------------------------------------------------------------------------------- /static/images/handlebars-devswag.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onface/handlebars-cn/HEAD/static/images/handlebars-devswag.png -------------------------------------------------------------------------------- /static/fonts/glyphicons-halflings-regular.eot: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onface/handlebars-cn/HEAD/static/fonts/glyphicons-halflings-regular.eot -------------------------------------------------------------------------------- /static/fonts/glyphicons-halflings-regular.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onface/handlebars-cn/HEAD/static/fonts/glyphicons-halflings-regular.ttf -------------------------------------------------------------------------------- /static/fonts/glyphicons-halflings-regular.woff: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onface/handlebars-cn/HEAD/static/fonts/glyphicons-halflings-regular.woff -------------------------------------------------------------------------------- /static/images/forkme_right_darkblue_121621.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onface/handlebars-cn/HEAD/static/images/forkme_right_darkblue_121621.png -------------------------------------------------------------------------------- /static/fonts/glyphicons-halflings-regular.woff2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/onface/handlebars-cn/HEAD/static/fonts/glyphicons-halflings-regular.woff2 -------------------------------------------------------------------------------- /s.js: -------------------------------------------------------------------------------- 1 | var app = require('express')() 2 | app.use(require('express').static('./')) 3 | var port = 4784 4 | app.listen(port, function () { 5 | console.log('127.0.0.1:' + port) 6 | }) 7 | -------------------------------------------------------------------------------- /posts/reference.md: -------------------------------------------------------------------------------- 1 | 12 | 13 |
5 |
6 | [handlebras](http://handlebarsjs.com/) 官方文档的中文版
7 |
8 | - 中文版:[http://handlebars-cn.onface.live/](http://handlebars-cn.onface.live//)
9 | - 英文版:[http://www.handlebarsjs.com](http://www.handlebarsjs.com/)
10 |
11 | ## 直接提交 [issues](https://github.com/onface/handlebars-cn/issues) 报告翻译错误
12 | 打开 [issues](https://github.com/onface/handlebars-cn/issues/new) ,填写翻译错误对应的 URL 地址、错误文字、正确文字,原因。
13 |
14 | ## 使用 gulp 参与编辑
15 |
16 | 1. Fork 此项目
17 | 1. 使用 Github Clone 到本地
18 | 2. 打开命令行/终端
19 | 3. 跳转至对应目录
20 | 4. 命令行输入 `npm install`
21 | 5. 输入 `gulp` 回车
22 | 6. 开始监控 `/post/**/*.md` 文件修改,当 `*.md` 文件被修改时,将自动同步 对应的 `/**/*.html` 文件。
23 |
24 | 修改完成后使用 github 发送 pull requests
25 |
26 | ### HTML 特殊处理
27 |
28 | 因为官方英文文档中夹杂着一些带 class 的 `
16 |
17 |
21 |
22 | {{content}}
23 |
24 | 发现翻译错误?告诉我们。
25 |
26 |
31 |
32 |
33 |
34 |
--------------------------------------------------------------------------------
/posts/demo/SafeString-escapeExpression.md:
--------------------------------------------------------------------------------
1 |
12 |
13 |
14 | 使用 `escapeExpression` 配合 `SafeString` 输出链接
15 |
16 |
17 |
18 |
19 |
24 |
25 |
26 |
27 |
37 |
38 |
39 |
--------------------------------------------------------------------------------
/posts/execution.md:
--------------------------------------------------------------------------------
1 |
12 |
13 |
14 |
15 | 传入数据,执行 Handlebars 返回 渲染后的 HTML。
16 |
17 | ```html
18 | var source = $("#entry-template").html();
19 | var template = Handlebars.compile(source);
20 |
21 | var context = {title: "My New Post", body: "This is my first post!"}
22 | var html = template(context);
23 | ```
24 | 渲染结果
25 |
26 | ```html
27 |
16 |
17 |
21 |
22 |
23 |
38 |
39 |
40 |
41 |
--------------------------------------------------------------------------------
/block_helpers.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
16 |
17 |
21 |
22 |
23 |
38 |
39 |
40 |
41 |
--------------------------------------------------------------------------------
/precompilation.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
16 |
17 |
21 |
22 |
23 |
38 |
39 |
40 |
41 |
--------------------------------------------------------------------------------
/builtin_helpers.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
16 |
17 |
21 |
22 |
23 |
38 |
39 |
40 |
41 |
--------------------------------------------------------------------------------
/demo/HTML-escapes.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 | 将模板修改为 {{{html}}} 可查看避免转义后的结果
使用 escapeExpression 配合 SafeString 输出链接
如果你的表达式的标识符是特殊字符,可以使用 [ 和 ] 包裹标识符
25 | 26 | 36 | 37 |(注意:为了查看方便,此示例将 10 改为 1 )
16 |
17 |
21 |
22 |
23 | 传入数据,执行 Handlebars 返回 渲染后的 HTML。
25 |var source = $("#entry-template").html();
26 | var template = Handlebars.compile(source);
27 |
28 | var context = {title: "My New Post", body: "This is my first post!"}
29 | var html = template(context);
30 |
31 | 渲染结果
32 |<div class="entry">
33 | <h1>My New Post</h1>
34 | <div class="body">
35 | This is my first post!
36 | </div>
37 | </div>
38 |
39 |
40 | 这里的 template() 允许传入第二个参数作为选项
The template function can be passed an options object as the second parameter which allows for customization:
44 |data Pass in an object to define custom @variable private variables.helpers Pass in to provide custom helpers in addition to the globally defined helpers.
47 | Values defined in this object will replace any values defined in the global object for the duration of the template execution.partials Pass in to provide custom partials in addition to the globally defined partials.
49 | Values defined in this object will replace any values defined in the global object for the duration of the template execution.
60 |
61 |
62 |
63 |
--------------------------------------------------------------------------------
/static/stylesheets/sunburst.css:
--------------------------------------------------------------------------------
1 | pre.sunburst {
2 | padding: 10px;
3 | font-size: 13px;
4 | font-family: Monaco, monospace;
5 | }
6 |
7 | pre.sunburst .DiffInserted {
8 | background-color: #253B22;
9 | color: #F8F8F8;
10 | }
11 | pre.sunburst .DiffHeader {
12 | background-color: #0E2231;
13 | color: #F8F8F8;
14 | font-style: italic;
15 | }
16 | pre.sunburst .CssPropertyValue {
17 | color: #F9EE98;
18 | }
19 | pre.sunburst .CCCPreprocessorDirective {
20 | color: #AFC4DB;
21 | }
22 | pre.sunburst .Constant {
23 | color: #3387CC;
24 | }
25 | pre.sunburst .DiffChanged {
26 | background-color: #4A410D;
27 | color: #F8F8F8;
28 | }
29 | pre.sunburst .Support {
30 | color: #9B859D;
31 | }
32 | pre.sunburst .MarkupList {
33 | color: #E1D4B9;
34 | }
35 | pre.sunburst .CssConstructorArgument {
36 | color: #8F9D6A;
37 | }
38 | pre.sunburst .Storage {
39 | color: #99CF50;
40 | }
41 | pre.sunburst .line-numbers {
42 | background-color: #DDF0FF;
43 | color: #000000;
44 | }
45 | pre.sunburst .CssClass {
46 | color: #9B703F;
47 | }
48 | pre.sunburst .StringConstant {
49 | color: #DDF2A4;
50 | }
51 | pre.sunburst .MarkupSeparator {
52 | background-color: #242424;
53 | color: #60A633;
54 | }
55 | pre.sunburst .MarkupUnderline {
56 | text-decoration: underline;
57 | color: #E18964;
58 | }
59 | pre.sunburst .CssAtRule {
60 | color: #8693A5;
61 | }
62 | pre.sunburst .MetaTagInline {
63 | color: #E0C589;
64 | }
65 | pre.sunburst .JEntityNameType {
66 | text-decoration: underline;
67 | }
68 | pre.sunburst .LogEntryError {
69 | background-color: #751012;
70 | }
71 | pre.sunburst .MarkupHeading {
72 | background-color: #632D04;
73 | color: #FEDCC5;
74 | }
75 | pre.sunburst .CssTagName {
76 | color: #CDA869;
77 | }
78 | pre.sunburst .SupportConstant {
79 | color: #CF6A4C;
80 | }
81 | pre.sunburst .MarkupQuote {
82 | background-color: #ECD091;
83 | color: #E1D4B9;
84 | font-style: italic;
85 | }
86 | pre.sunburst .DiffDeleted {
87 | background-color: #420E09;
88 | color: #F8F8F8;
89 | }
90 | pre.sunburst .CCCPreprocessorLine {
91 | color: #8996A8;
92 | }
93 | pre.sunburst .StringRegexpSpecial {
94 | color: #CF7D34;
95 | }
96 | pre.sunburst .EmbeddedSourceBright {
97 | background-color: #292929;
98 | }
99 | pre.sunburst .InvalidIllegal {
100 | background-color: #150B15;
101 | color: #FD5FF1;
102 | }
103 | pre.sunburst .MarkupRaw {
104 | background-color: #ABADB4;
105 | color: #578BB3;
106 | }
107 | pre.sunburst .SupportFunction {
108 | color: #DAD085;
109 | }
110 | pre.sunburst .CssAdditionalConstants {
111 | color: #DD7B3B;
112 | }
113 | pre.sunburst .MetaTagAll {
114 | color: #89BDFF;
115 | }
116 | pre.sunburst .StringRegexp {
117 | color: #E9C062;
118 | }
119 | pre.sunburst .StringEmbeddedSource {
120 | color: #DAEFA3;
121 | }
122 | pre.sunburst .EntityInheritedClass {
123 | color: #9B5C2E;
124 | font-style: italic;
125 | }
126 | pre.sunburst .MarkupComment {
127 | color: #F67B37;
128 | font-style: italic;
129 | }
130 | pre.sunburst .MarkupBold {
131 | font-weight: bold;
132 | color: #E9C062;
133 | }
134 | pre.sunburst .CssId {
135 | color: #8B98AB;
136 | }
137 | pre.sunburst .CssPseudoClass {
138 | color: #8F9D6A;
139 | }
140 | pre.sunburst .JCast {
141 | color: #676767;
142 | font-style: italic;
143 | }
144 | pre.sunburst .StringVariable {
145 | color: #8A9A95;
146 | }
147 | pre.sunburst .String {
148 | color: #65B042;
149 | }
150 | pre.sunburst .Keyword {
151 | color: #E28964;
152 | }
153 | pre.sunburst {
154 | background-color: #000000;
155 | color: #F8F8F8;
156 | }
157 | pre.sunburst .LogEntry {
158 | background-color: #C7C7C7;
159 | }
160 | pre.sunburst .MarkupItalic {
161 | color: #E9C062;
162 | font-style: italic;
163 | }
164 | pre.sunburst .CssPropertyName {
165 | color: #C5AF75;
166 | }
167 | pre.sunburst .Namespaces {
168 | color: #E18964;
169 | }
170 | pre.sunburst .DoctypeXmlProcessing {
171 | color: #494949;
172 | }
173 | pre.sunburst .InvalidDeprecated {
174 | color: #FD5FF1;
175 | font-style: italic;
176 | }
177 | pre.sunburst .Variable {
178 | color: #3E87E3;
179 | }
180 | pre.sunburst .Entity {
181 | color: #89BDFF;
182 | }
183 | pre.sunburst .Comment {
184 | color: #AEAEAE;
185 | font-style: italic;
186 | }
187 |
--------------------------------------------------------------------------------
/README.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
16 |
17 |
21 |
22 | handlebras 官方文档的中文版
25 |打开 issues ,填写翻译错误对应的 URL 地址、错误文字、正确文字,原因。
31 |npm installgulp 回车/post/**/*.md 文件修改,当 *.md 文件被修改时,将自动同步 对应的 /**/*.html 文件。修改完成后使用 github 发送 pull requests
42 |因为官方英文文档中夹杂着一些带 class 的 <div> 所以通过 replace 做了一个替换。
<!---<div class="bullet">-->
45 |
46 | Handlebars 模板看起来很像 HTML ,Handlebars 表达式嵌入在 HTML 中。
47 |
48 | html = html.replace(/<!---([^-]+)-->/g,'$1')
49 |
50 | 可查看 /posts/index.md 的源码以理解 <!---<tag>--> 的用法。
60 |
61 |
62 |
63 |
--------------------------------------------------------------------------------
/gulpfile.js:
--------------------------------------------------------------------------------
1 | var config = {
2 | anchorClass : "glyphicon glyphicon-link"
3 | }
4 |
5 | var gulp = require('gulp')
6 | var markdown = require('gulp-markdown')
7 | var tap = require('gulp-tap')
8 | var gutil = require('gulp-util')
9 | var cheerio = require('gulp-cheerio')
10 | var highlight = require('highlight.js')
11 |
12 | var fs = require('fs')
13 | var Handlebars = require('handlebars')
14 | var notifier = require('node-notifier')
15 | var path = require('path')
16 | var handlebarshelperis = require('handlebars-helper-is')
17 | handlebarshelperis(Handlebars)
18 | var server = require('gulp-server-livereload')
19 |
20 | // .replace(rStringToRegExp,'\\$1'))
21 | // "[]" => "\[\]"
22 | var rStringToRegExp = /([.$^{[(|)*+?\/])/g
23 | var fStringToPrefixRegExp = function (str) {
24 | return new RegExp(
25 | '^' + (
26 | str.replace(rStringToRegExp, '\\$1')
27 | )
28 | );
29 | }
30 |
31 | // 渲染数据源:为了让-include 能访问数据源,每次渲染都将数据源绑定在此对象上。
32 | var oRenderData = {}
33 |
34 | /*!
35 | 让模板支持引入其他模板作为模块
36 | {{-include header}}
37 | 加载 _template/header.html 到当前代码行
38 | */
39 | Handlebars.registerHelper('include', function (path) {
40 | path = '_template/' + path + '.html'
41 | if (fs.existsSync(path)) {
42 | return Handlebars.compile(
43 | new Handlebars.SafeString(
44 | fs.readFileSync(path, 'utf-8')
45 | ).toString()
46 | )(oRenderData)
47 | } else {
48 | var message = "Not find: {{include " + path + '}}'
49 | gutil.log(gutil.colors.red(message))
50 | notifier.notify({
51 | 'title': 'Gulp Not find',
52 | 'message': message
53 | });
54 | return new Handlebars.SafeString('' + message + '')
55 | }
56 | })
57 |
58 | /*!
59 | 比较2个参数
60 | {{-is a b "==" "!=="}}
61 | */
62 | Handlebars.registerHelper('-is', function (a, b, yes, no) {
63 | return a == b?yes:no;
64 | })
65 |
66 |
67 | /*!
68 | 编译 markdown
69 | @paths {string|array} - glob 或一个文件的绝对路径 gulp.src(paths)
70 | @dist {string} - 相当于 gulp.dest(dist) 中的 dist
71 | */
72 |
73 | var compileMD = function (paths) {
74 | gulp.src(paths)
75 | .pipe(markdown({
76 | highlight: function (code) {
77 | return highlight.highlightAuto(code).value
78 | }
79 | }))
80 | .pipe(cheerio(function($, file){
81 | var $titles = $('h1,h2,h3,h4,h5,h6')
82 | $titles.removeAttr('id')
83 | var $a = $('a')
84 | $a.each(function () {
85 | var $this = $(this)
86 | var target = $this.attr('target');
87 | if (!target){
88 | $this.attr('target', '_blank')
89 | }
90 | })
91 | }))
92 | .pipe(tap(function (file) {
93 | oRenderData = {
94 | URL_PATH: file.path.replace(fStringToPrefixRegExp(__dirname),'')
95 | }
96 | var html = file.contents.toString()
97 | // 将 替换为
98 | html = html.replace(/(href=['"][^"']+\.)md(['"])/g,'$1html$2')
99 | // 将 转换为锚记
100 | // oHash 用于防止 hash 重复
101 | html = html.replace(//g,'$1')
102 | // 将 替换为 `
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 | 如果你的标识符是特殊字符,可以使用 `[` 和 `]` 包裹标识符:
65 |
66 | ```html
67 | {{#each articles.[10].[#comments]}}
68 | 99 | & 会被转换为 & 100 | < ~ < 101 | > ~ > 102 | " ~ " 103 | ' ~ ' 104 | ` ~ ` 105 |106 | 107 |
[@contextPath](reference.html#data-contextPath) variable that denotes the lookup path for the current context. It's highly recommended that generic helpers provide such a variable if they modify the context when executing their children.
379 |
380 |
381 |
382 |
383 |
--------------------------------------------------------------------------------
/posts/index.md:
--------------------------------------------------------------------------------
1 |
12 |
13 | Tags", 121 | body: "
This is a post about <p> tags
" 122 | } 123 | ``` 124 | 125 | 渲染结果: 126 | 127 | ```html 128 |This is a post about <p> tags
132 |{{name}}
218 | ``` 219 | 220 | Handlebars also supports nested paths, making it possible to look up properties nested below the current context. 221 | 222 | ```html 223 |{{./name}} or {{this/name}} or {{this.name}}
274 | ``` 275 | 276 | Any of the above would cause the `name` field on the current context to be used rather than a helper of the same name. 277 | 278 | 279 | Template comments with {{!-- --}} or {{! }}. 280 | --------------------------------------------- 281 | 282 | You can use comments in your handlebars code just as you would in your code. Since there is generally some level of logic, this is a good practice. 283 | 284 | ```html 285 |最简单的表达式是通过 {{ 和 }} 包裹数据属性名
<h1>{{title}}</h1>
33 |
34 | {{title}}会查找当前上下文 (上下文?) 中的 title 属性。 Block helpers 会改变当前上下文,但他不会改变表达式的语法。
Actually, it means "look for a helper named title, then do the above", but we'll get to that soon enough.
36 |你还可以使用 . 查找属性的子元素
<h1>{{article.title}}</h1>
40 |
41 | 这个示例的意思是:寻找当前上下文中的 article 属性,然后查找 article 属性的 title 属性。找到后输出 title
handlebars也支持已经弃用的 / 分隔符,上面的表达式也可以写成:
<h1>{{article/title}}</h1>
44 |
45 | 标识符可以是除了以下字符以外的任何 unicode 字符:
48 |空白 ! " # % & ' ( ) * + , . / ; < = > @ [ \ ] ^ { | } ~ `
如果你的标识符是特殊字符,可以使用 [ 和 ] 包裹标识符:
{{#each articles.[10].[#comments]}}
54 | <h1>{{subject}}</h1>
55 | <div>
56 | {{body}}
57 | </div>
58 | {{/each}}
59 |
60 | 模板中的 articles.[10].[#comments] 相当于 JavaScript 中的 object.articles[10]["#comments"]。示例
articles 也可以是对象,如果是对象则访问 articles 的 10 属性。
63 | You may not include a closing ] in a path-literal, but all other characters are fair game.
Handlebars 会转义HTML,你可以使用 {{{ 避免转义
{{{foo}}}
71 |
72 | 75 | & 会被转换为 & 76 | < ~ < 77 | > ~ > 78 | " ~ " 79 | ' ~ ' 80 | ` ~ ` 81 |82 | 83 |
{{{link story}}}
94 | 如上示例,link 是 helper 的名称,story 是 helper 的参数。Handlebars evaluates parameters in exactly the same way described above in "Basic Usage".
Handlebars.registerHelper('link', function(object) {
97 | var url = Handlebars.escapeExpression(object.url),
98 | text = Handlebars.escapeExpression(object.text);
99 |
100 | return new Handlebars.SafeString(
101 | "<a href='" + url + "'>" + text + "</a>"
102 | );
103 | });
104 | 当一个 helper 返回 HTML 字符串 时。你应该使用 SafeString 来避免转义,确保输出的是可渲染的HTML。
当使用 SafeString 时,应主动使用 escapeExpression 方法将不安全的数据过滤。
<a> 而不是 <a>,<span onclick="javascript:alert(1);">点击我</span> 这这危险数据。你也可以向 helper 传入字符串、数字或布尔值
113 |{{{link "See more..." story.url}}}
114 | 在这种情况下,把手将通过链接帮手两个参数:字符串“查看更多......”而在当前的背景下评估story.url的结果。
115 |此时,Handlebars 会将字符串参数 "See more..." 和当前上下文的 story.url 传递给 helper link。
Handlebars.registerHelper('link', function(text, url) {
117 | url = Handlebars.escapeExpression(url);
118 | text = Handlebars.escapeExpression(text);
119 |
120 | return new Handlebars.SafeString(
121 | "<a href='" + url + "'>" + text + "</a>"
122 | );
123 | });
124 |
125 |
126 | 你还可以使用 story.text 来渲染动态文本
{{{link story.text story.url}}}
128 |
129 | Handlebars helpers 支持传入任意顺序的 key-value (在本文档中被称为 hash arguments):
133 |{{{link "See more..." href=story.url class="story"}}}
134 | href=story.url class="story"。
136 |
137 | href是 key story.url 是 valueclass是 key "story" 是 valuehash arguments 必须是简单的标识符,value 必须是表达式。value 的值可以是标识符、路径和字符串。
145 |Handlebars.registerHelper('link', function(text, options) {
146 | var attrs = [];
147 |
148 | for (var prop in options.hash) {
149 | attrs.push(
150 | Handlebars.escapeExpression(prop) + '="'
151 | + Handlebars.escapeExpression(options.hash[prop]) + '"');
152 | }
153 |
154 | return new Handlebars.SafeString(
155 | "<a " + attrs.join(" ") + ">" + Handlebars.escapeExpression(text) + "</a>"
156 | );
157 | });
158 | 最终渲染结果:
160 |<a class="story" href="http://http://handlebars-cn.onface.live/">See more...</a>
161 | 上例中的 options.hash 可访问 hash arguments 。示例
Handlebars 支持子表达式, 子表达式的结果可作为父表达式的参数,字表达式使用括号分割 (。
{{outer-helper (inner-helper 'abc') 'def'}}
176 | 在这个示例中,'abc' 作为 inner-helper 的参数被调用,而 inner-helper 的返回值作为 outer-helper 的第一个参数被调用('def' 作为第二个参数被调用)。
模板空白可由任何 mustache 语法括号中任意一侧添加 ~ 字符以删除。When applied all whitespace on that side will be removed up to the first handlebars expression or non-whitespace character on that side.
{{#each nav ~}}
187 | <a href="{{url}}">
188 | {{~#if test}}
189 | {{~title}}
190 | {{~else~}}
191 | Empty
192 | {{~/if~}}
193 | </a>
194 | {{~/each}}
195 |
196 | 数据为:
197 |{
198 | "nav": [
199 | {"url": "foo", "test": true, "title": "bar"},
200 | {"url": "bar"}
201 | ]
202 | }
203 |
204 | 最终会输出无换行和空格的内容
205 |<a href="foo">bar</a><a href="bar">Empty</a>
206 |
207 |
208 |
209 | This expands the default behavior of stripping lines that are "standalone" helpers (only a block helper, comment, or partial and whitespace).
210 |{{#each nav}}
211 | <a href="{{url}}">
212 | {{#if test}}
213 | {{title}}
214 | {{else}}
215 | Empty
216 | {{/if}}
217 | </a>
218 | {{~/each}}
219 |
220 | 渲染结果:
221 |<a href="foo">
222 | bar
223 | </a>
224 | <a href="bar">
225 | Empty
226 | </a>
227 |
228 |
229 | Optionally, helpers can be informed of the paths that were used to lookup an argument for a given value. This mode may be enabled via the trackIds compiler flag.
{{foo bar.baz}}
237 | would call the helper foo with the value of bar.baz but also will include the literal string "bar.baz" in the ids field on the options argument.
This can be used for future lookup of parameters should it be necessary, but does add additional overhead.
239 |When this mode is enabled, all builtin helpers will generate a @contextPath variable that denotes the lookup path for the current context. It's highly recommended that generic helpers provide such a variable if they modify the context when executing their children.
250 |
251 |
{{title}}
259 |