summaryrefslogtreecommitdiffstats
path: root/expressions.html
blob: 48c30588d870855247ab51d24b8a3ac815a1d4e9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<!DOCTYPE html>
<html lang="en">
  <head>
    <title>Handlebars 中文网:轻逻辑语义化的模板引擎</title>
    <meta charset="UTF-8">
    <link href="static/stylesheets/application.css" media="all" rel="stylesheet" type="text/css"/>
    <link href="static/stylesheets/sunburst.css" media="all" rel="stylesheet" type="text/css"/>
    <link href="static/stylesheets/site.css" media="all" rel="stylesheet" type="text/css"/>
    <link href="static/images/favicon.png" rel="shortcut icon" type="image/png" />
  </head>
  <body>
    <div id="body">
      <div id="header">
        <a href="index.html">
          <img alt="Handlebars 中文网:轻逻辑语义化的模板引擎" src="static/images/handlebars_logo.png"/>
        </a>
      </div>
      <div id="contents">
        <a href="http://www.devswag.com/collections/handlebars" id="callout">
          <img alt="Buy Handlebars swag on DevSwag!" src="static/images/handlebars-devswag.png"/>
        </a>
        
<div id="intro">
&#x8868;&#x8FBE;&#x5F0F;&#x662F; Handlebars &#x6A21;&#x677F;&#x7684;&#x57FA;&#x672C;&#x7EC4;&#x6210;&#x5355;&#x4F4D;&#xFF0C;&#x5355;&#x72EC;&#x4F7F;&#x7528; <code>{{mustache}}</code>&#xFF0C;pass them to a Handlebars helper, or use them as values in hash arguments.
</div>

<h2>&#x57FA;&#x672C;&#x7528;&#x6CD5;</h2>
<!-- Start .contents-->
<dic class="contents">
<div class="bullet">
<p>&#x6700;&#x7B80;&#x5355;&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#x662F;&#x901A;&#x8FC7; <code>{{</code> &#x548C; <code>}}</code> &#x5305;&#x88F9;&#x6570;&#x636E;&#x5C5E;&#x6027;&#x540D;</p>
<pre><code class="lang-html"><span class="xml"><span class="hljs-tag">&lt;<span class="hljs-title">h1</span>&gt;</span></span><span class="hljs-expression">{{<span class="hljs-variable">title</span>}}</span><span class="xml"><span class="hljs-tag">&lt;/<span class="hljs-title">h1</span>&gt;</span></span>
</code></pre>
<p><code>{{title}}</code>&#x4F1A;&#x67E5;&#x627E;&#x5F53;&#x524D;&#x4E0A;&#x4E0B;&#x6587; <a href="#" target="_blank">&#xFF08;&#x4E0A;&#x4E0B;&#x6587;&#xFF1F;&#xFF09;</a> &#x4E2D;&#x7684; <code>title</code> &#x5C5E;&#x6027;&#x3002; Block helpers &#x4F1A;&#x6539;&#x53D8;&#x5F53;&#x524D;&#x4E0A;&#x4E0B;&#x6587;&#xFF0C;&#x4F46;&#x4ED6;&#x4E0D;&#x4F1A;&#x6539;&#x53D8;&#x8868;&#x8FBE;&#x5F0F;&#x7684;&#x8BED;&#x6CD5;&#x3002;</p>
<p>Actually, it means &quot;look for a helper named title, then do the above&quot;, but we&apos;ll get to that soon enough.</p>
</div>
<div class="bullet">
<p>&#x4F60;&#x8FD8;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>.</code> &#x67E5;&#x627E;&#x5C5E;&#x6027;&#x7684;&#x5B50;&#x5143;&#x7D20;</p>
<pre><code class="lang-html"><span class="xml"><span class="hljs-tag">&lt;<span class="hljs-title">h1</span>&gt;</span></span><span class="hljs-expression">{{<span class="hljs-variable">article.title</span>}}</span><span class="xml"><span class="hljs-tag">&lt;/<span class="hljs-title">h1</span>&gt;</span></span>
</code></pre>
<p>&#x8FD9;&#x4E2A;&#x793A;&#x4F8B;&#x7684;&#x610F;&#x601D;&#x662F;&#xFF1A;&#x5BFB;&#x627E;&#x5F53;&#x524D;&#x4E0A;&#x4E0B;&#x6587;&#x4E2D;&#x7684; <code>article</code> &#x5C5E;&#x6027;&#xFF0C;&#x7136;&#x540E;&#x67E5;&#x627E; <code>article</code> &#x5C5E;&#x6027;&#x7684; <code>title</code> &#x5C5E;&#x6027;&#x3002;&#x627E;&#x5230;&#x540E;&#x8F93;&#x51FA; <code>title</code></p>
<p>handlebars&#x4E5F;&#x652F;&#x6301;&#x5DF2;&#x7ECF;&#x5F03;&#x7528;&#x7684; <code>/</code> &#x5206;&#x9694;&#x7B26;&#xFF0C;&#x4E0A;&#x9762;&#x7684;&#x8868;&#x8FBE;&#x5F0F;&#x4E5F;&#x53EF;&#x4EE5;&#x5199;&#x6210;&#xFF1A;</p>
<pre><code class="lang-html"><span class="xml"><span class="hljs-tag">&lt;<span class="hljs-title">h1</span>&gt;</span></span><span class="hljs-expression">{{<span class="hljs-variable">article</span><span class="hljs-end-block">/title</span>}}</span><span class="xml"><span class="hljs-tag">&lt;/<span class="hljs-title">h1</span>&gt;</span></span>
</code></pre>
</div>
<div class="bullet">
<p>&#x6807;&#x8BC6;&#x7B26;&#x53EF;&#x4EE5;&#x662F;&#x9664;&#x4E86;&#x4EE5;&#x4E0B;&#x5B57;&#x7B26;&#x4EE5;&#x5916;&#x7684;&#x4EFB;&#x4F55; unicode &#x5B57;&#x7B26;:</p>
<p>&#x7A7A;&#x767D; <code>!</code> <code>&quot;</code> <code>#</code> <code>%</code> <code>&amp;</code> <code>&apos;</code> <code>(</code> <code>)</code> <code>*</code> <code>+</code> <code>,</code> <code>.</code> <code>/</code> <code>;</code> <code>&lt;</code> <code>=</code> <code>&gt;</code> <code>@</code> <code>[</code> <code>\</code> <code>]</code> <code>^</code> <code>{</code> <code>|</code> <code>}</code> <code>~</code> <code>`</code></p>
</div>
<div class="bullet"
<p><a name="property-not-a-valid-identifier.html" target="_blank"></a></p>
<p>&#x5982;&#x679C;&#x4F60;&#x7684;&#x6807;&#x8BC6;&#x7B26;&#x662F;&#x7279;&#x6B8A;&#x5B57;&#x7B26;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>[</code> &#x548C; <code>]</code> &#x5305;&#x88F9;&#x6807;&#x8BC6;&#x7B26;&#xFF1A;</p>
<pre><code class="lang-html"><span class="xml"></span><span class="hljs-expression">{{<span class="hljs-begin-block">#<span class="hljs-keyword">each</span> articles.</span>[10]<span class="hljs-variable">.</span>[<span class="hljs-begin-block">#comments</span>]}}</span><span class="xml">
  <span class="hljs-tag">&lt;<span class="hljs-title">h1</span>&gt;</span></span><span class="hljs-expression">{{<span class="hljs-variable">subject</span>}}</span><span class="xml"><span class="hljs-tag">&lt;/<span class="hljs-title">h1</span>&gt;</span>
  <span class="hljs-tag">&lt;<span class="hljs-title">div</span>&gt;</span>
    </span><span class="hljs-expression">{{<span class="hljs-variable">body</span>}}</span><span class="xml">
  <span class="hljs-tag">&lt;/<span class="hljs-title">div</span>&gt;</span>
</span><span class="hljs-expression">{{<span class="hljs-end-block">/<span class="hljs-keyword">each</span></span>}}</span><span class="xml"></span>
</code></pre>
<p>&#x6A21;&#x677F;&#x4E2D;&#x7684; <code>articles.[10].[#comments]</code> &#x76F8;&#x5F53;&#x4E8E; JavaScript &#x4E2D;&#x7684; <code>object.articles[10][&quot;#comments&quot;]</code>&#x3002;<a href="demo/property-not-a-valid-identifier.html" target="_blank">&#x793A;&#x4F8B;</a></p>
<div class="translator">
<code>articles</code> &#x4E5F;&#x53EF;&#x4EE5;&#x662F;&#x5BF9;&#x8C61;&#xFF0C;&#x5982;&#x679C;&#x662F;&#x5BF9;&#x8C61;&#x5219;&#x8BBF;&#x95EE; <code>articles</code> &#x7684; <code>10</code> &#x5C5E;&#x6027;&#x3002;
</div>

<p>You may not include a closing <code>]</code> in a path-literal, but all other characters are fair game.</p>
</div>
<div class="bullet">
<p><a name="HTML-escapes.html" target="_blank"></a></p>
<p>Handlebars &#x4F1A;&#x8F6C;&#x4E49;HTML&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>{{{</code> &#x907F;&#x514D;&#x8F6C;&#x4E49;</p>
<pre><code><span class="hljs-collection">{<span class="hljs-collection">{<span class="hljs-collection">{foo}</span>}</span>}</span>
</code></pre><p><a href="demo/HTML-escapes.html" target="_blank">&#x793A;&#x4F8B;</a></p>
<div class="translator">
&#x8F6C;&#x4E49;HTML &#x6307;&#x7684;&#x662F;:
<pre>
&amp; &#x4F1A;&#x88AB;&#x8F6C;&#x6362;&#x4E3A; &amp;amp;
&lt; ~ &amp;lt;
&gt; ~ &amp;gt;
&quot; ~ &amp;quot;
&apos; ~ &amp;#x27;
` ~ &amp;#x60;
</pre>

</div>


</div>
<!-- End .contents-->
<h2>Helpers</h2>
<!-- Start .contents-->
<dic class="contents">
<div class="bullet"
<p>Helper &#x8BED;&#x6CD5;&#x662F;&#x7B80;&#x5355;&#x7684;&#x6807;&#x8BC6;&#x7B26;&#x540E;&#x9762;&#x7D27;&#x8DDF;&#x7740;&#x4E00;&#x4E2A;&#x6216;&#x591A;&#x4E2A;&#x53C2;&#x6570;&#xFF08;&#x7528;&#x7A7A;&#x683C;&#x5206;&#x9694;&#xFF09;&#xFF0C;&#x6BCF;&#x4E2A;&#x53C2;&#x6570;&#x90FD;&#x662F; Handlebars &#x8868;&#x8FBE;&#x5F0F;&#x3002;</p>
<pre><code><span class="hljs-collection">{<span class="hljs-collection">{<span class="hljs-collection">{link story}</span>}</span>}</span>
</code></pre><p>&#x5982;&#x4E0A;&#x793A;&#x4F8B;&#xFF0C;<code>link</code> &#x662F; helper &#x7684;&#x540D;&#x79F0;&#xFF0C;<code>story</code> &#x662F; helper &#x7684;&#x53C2;&#x6570;&#x3002;Handlebars evaluates parameters in exactly the same way described above in &quot;Basic Usage&quot;.</p>
<p><a name="SafeString-escapeExpression" target="_blank"></a></p>
<pre><code><span class="hljs-type">Handlebars</span>.registerHelper(<span class="hljs-symbol">&apos;lin</span>k&apos;, function(<span class="hljs-class"><span class="hljs-keyword">object</span>) {</span>
  <span class="hljs-keyword">var</span> url = <span class="hljs-type">Handlebars</span>.escapeExpression(<span class="hljs-class"><span class="hljs-keyword">object</span>.<span class="hljs-title">url</span>),
</span>      text = <span class="hljs-type">Handlebars</span>.escapeExpression(<span class="hljs-class"><span class="hljs-keyword">object</span>.<span class="hljs-title">text</span>);</span>

  <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-type">Handlebars</span>.<span class="hljs-type">SafeString</span>(
    <span class="hljs-string">&quot;&lt;a href=&apos;&quot;</span> + url + <span class="hljs-string">&quot;&apos;&gt;&quot;</span> + text + <span class="hljs-string">&quot;&lt;/a&gt;&quot;</span>
  );
});
</code></pre><p>&#x5F53;&#x4E00;&#x4E2A; helper &#x8FD4;&#x56DE; HTML &#x5B57;&#x7B26;&#x4E32; &#x65F6;&#x3002;&#x4F60;&#x5E94;&#x8BE5;&#x4F7F;&#x7528; <code>SafeString</code> &#x6765;&#x907F;&#x514D;&#x8F6C;&#x4E49;&#xFF0C;&#x786E;&#x4FDD;&#x8F93;&#x51FA;&#x7684;&#x662F;&#x53EF;&#x6E32;&#x67D3;&#x7684;HTML&#x3002;<br>&#x5F53;&#x4F7F;&#x7528; <code>SafeString</code> &#x65F6;&#xFF0C;&#x5E94;&#x4E3B;&#x52A8;&#x4F7F;&#x7528; <code>escapeExpression</code> &#x65B9;&#x6CD5;&#x5C06;&#x4E0D;&#x5B89;&#x5168;&#x7684;&#x6570;&#x636E;&#x8FC7;&#x6EE4;&#x3002;</p>
<div class="translator">
&#x786E;&#x4FDD;&#x8F93;&#x51FA;<code>&lt;a&gt;</code> &#x800C;&#x4E0D;&#x662F; <code>&amp;lt;a&amp;gt;</code>&#xFF0C;<br>&#x5E76;&#x8FC7;&#x6EE4; <code>&lt;span onclick=&quot;javascript:alert(1);&quot;&gt;&#x70B9;&#x51FB;&#x6211;&lt;/span&gt;</code> &#x8FD9;&#x8FD9;&#x5371;&#x9669;&#x6570;&#x636E;&#x3002;<br>&#x82E5;&#x4E0D;&#x660E;&#x767D;&#x8BF7;&#x770B; <a href="demo/SafeString-escapeExpression.html" target="_blank">&#x793A;&#x4F8B;</a>
</div>

</div>
<div class="bullet"
<p><a name="expressions-1.html" target="_blank"></a></p>
<p>&#x4F60;&#x4E5F;&#x53EF;&#x4EE5;&#x5411; helper &#x4F20;&#x5165;&#x5B57;&#x7B26;&#x4E32;&#x3001;&#x6570;&#x5B57;&#x6216;&#x5E03;&#x5C14;&#x503C;</p>
<pre><code><span class="hljs-collection">{<span class="hljs-collection">{<span class="hljs-collection">{link <span class="hljs-string">&quot;See more...&quot;</span> story.url}</span>}</span>}</span>
</code></pre><p>&#x5728;&#x8FD9;&#x79CD;&#x60C5;&#x51B5;&#x4E0B;&#xFF0C;&#x628A;&#x624B;&#x5C06;&#x901A;&#x8FC7;&#x94FE;&#x63A5;&#x5E2E;&#x624B;&#x4E24;&#x4E2A;&#x53C2;&#x6570;&#xFF1A;&#x5B57;&#x7B26;&#x4E32;&#x201C;&#x67E5;&#x770B;&#x66F4;&#x591A;......&#x201D;&#x800C;&#x5728;&#x5F53;&#x524D;&#x7684;&#x80CC;&#x666F;&#x4E0B;&#x8BC4;&#x4F30;story.url&#x7684;&#x7ED3;&#x679C;&#x3002;</p>
<p>&#x6B64;&#x65F6;&#xFF0C;Handlebars &#x4F1A;&#x5C06;&#x5B57;&#x7B26;&#x4E32;&#x53C2;&#x6570; <code>&quot;See more...&quot;</code> &#x548C;&#x5F53;&#x524D;&#x4E0A;&#x4E0B;&#x6587;&#x7684; <code>story.url</code> &#x4F20;&#x9012;&#x7ED9; helper link&#x3002;</p>
<pre><code>Handlebars.registerHelper(<span class="hljs-string">&apos;link&apos;</span>, function(<span class="hljs-built_in">text</span>, url) {
  url = Handlebars.escapeExpression(url);
  <span class="hljs-built_in">text</span> = Handlebars.escapeExpression(<span class="hljs-built_in">text</span>);

  <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> Handlebars.SafeString(
    <span class="hljs-string">&quot;&lt;a href=&apos;&quot;</span> + url + <span class="hljs-string">&quot;&apos;&gt;&quot;</span> + <span class="hljs-built_in">text</span> + <span class="hljs-string">&quot;&lt;/a&gt;&quot;</span>
  );
});
</code></pre><p><a href="demo/expressions-1.html" target="_blank">&#x793A;&#x4F8B;</a></p>
<p><a name="expressions-2.html" target="_blank"></a></p>
<p>&#x4F60;&#x8FD8;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>story.text</code> &#x6765;&#x6E32;&#x67D3;&#x52A8;&#x6001;&#x6587;&#x672C;</p>
<pre><code><span class="xml"></span><span class="hljs-expression">{{{<span class="hljs-variable">link</span> <span class="hljs-variable">story.text</span> <span class="hljs-variable">story.url</span>}}</span><span class="xml">}</span>
</code></pre><p><a href="demo/expressions-2.html" target="_blank">&#x793A;&#x4F8B;</a></p>
</div>
<div class="bullet"
<p><a name="demo/expressions-3.html" target="_blank"></a></p>
<p>Handlebars helpers &#x652F;&#x6301;&#x4F20;&#x5165;&#x4EFB;&#x610F;&#x987A;&#x5E8F;&#x7684; key-value &#xFF08;&#x5728;&#x672C;&#x6587;&#x6863;&#x4E2D;&#x88AB;&#x79F0;&#x4E3A; hash arguments&#xFF09;&#xFF1A;</p>
<pre><code><span class="xml"></span><span class="hljs-expression">{{{<span class="hljs-variable">link</span> <span class="hljs-string">&quot;See more...&quot;</span> <span class="hljs-variable">href</span>=<span class="hljs-variable">story.url</span> <span class="hljs-variable">class</span>=<span class="hljs-string">&quot;story&quot;</span>}}</span><span class="xml">}</span>
</code></pre><div class="translator">
&#x6B64;&#x5904; hash arguments &#x662F; <code>href=story.url class=&quot;story&quot;</code>&#x3002;

<ul>
    <li><code>href</code>&#x662F; key <code>story.url</code> &#x662F; value</li>
    <li><code>class</code>&#x662F; key <code>&quot;story&quot;</code> &#x662F; value</li>
</ul>

</div>

<p>hash arguments &#x5FC5;&#x987B;&#x662F;&#x7B80;&#x5355;&#x7684;&#x6807;&#x8BC6;&#x7B26;&#xFF0C;value &#x5FC5;&#x987B;&#x662F;&#x8868;&#x8FBE;&#x5F0F;&#x3002;value &#x7684;&#x503C;&#x53EF;&#x4EE5;&#x662F;&#x6807;&#x8BC6;&#x7B26;&#x3001;&#x8DEF;&#x5F84;&#x548C;&#x5B57;&#x7B26;&#x4E32;&#x3002;</p>
<pre><code>Handlebars.registerHelper(<span class="hljs-string">&apos;link&apos;</span>, <span class="hljs-function"><span class="hljs-keyword">function</span><span class="hljs-params">(text, options)</span> </span>{
  <span class="hljs-keyword">var</span> attrs = [];

  <span class="hljs-keyword">for</span> (<span class="hljs-keyword">var</span> prop <span class="hljs-keyword">in</span> options.hash) {
    attrs.push(
        Handlebars.escapeExpression(prop) + <span class="hljs-string">&apos;=&quot;&apos;</span>
        + Handlebars.escapeExpression(options.hash[prop]) + <span class="hljs-string">&apos;&quot;&apos;</span>);
  }

  <span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> Handlebars.SafeString(
    <span class="hljs-string">&quot;&lt;a &quot;</span> + attrs.join(<span class="hljs-string">&quot; &quot;</span>) + <span class="hljs-string">&quot;&gt;&quot;</span> + Handlebars.escapeExpression(text) + <span class="hljs-string">&quot;&lt;/a&gt;&quot;</span>
  );
});
</code></pre><div class="translator">
<p>&#x6700;&#x7EC8;&#x6E32;&#x67D3;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code><span class="hljs-tag">&lt;<span class="hljs-title">a</span> <span class="hljs-attribute">class</span>=<span class="hljs-value">&quot;story&quot;</span> <span class="hljs-attribute">href</span>=<span class="hljs-value">&quot;http://handlebarsjs.org&quot;</span>&gt;</span>See more...<span class="hljs-tag">&lt;/<span class="hljs-title">a</span>&gt;</span>
</code></pre></div>
<p>&#x4E0A;&#x4F8B;&#x4E2D;&#x7684; <code>options.hash</code> &#x53EF;&#x8BBF;&#x95EE; hash arguments &#x3002;<a href="demo/expressions-3.html" target="_blank">&#x793A;&#x4F8B;</a></p>
</div>
<div class="bullet"
<p>Handlebars also offers a mechanism for invoking a helper with a block of the template. Block helpers can then invoke that block zero or more times with any context it chooses.</p>
<p><a href="block_helpers.html" class="more-info" target="_blank">&#x4E86;&#x89E3;&#x66F4;&#x591A;&#xFF1A;&#x5757; helpers</a></p>
</div>
<!-- End .contents-->
<p><a name="expressions-subexpressions.html" target="_blank"></a></p>
<h2>&#x5B50;&#x8868;&#x8FBE;&#x5F0F;</h2>
<!-- Start .contents-->
<dic class="contents">
<div class="bullet">
<p>Handlebars &#x652F;&#x6301;&#x5B50;&#x8868;&#x8FBE;&#x5F0F;, &#x5B50;&#x8868;&#x8FBE;&#x5F0F;&#x7684;&#x7ED3;&#x679C;&#x53EF;&#x4F5C;&#x4E3A;&#x7236;&#x8868;&#x8FBE;&#x5F0F;&#x7684;&#x53C2;&#x6570;&#xFF0C;&#x5B57;&#x8868;&#x8FBE;&#x5F0F;&#x4F7F;&#x7528;&#x62EC;&#x53F7;&#x5206;&#x5272; <code>(</code>&#x3002;</p>
<pre><code><span class="xml"></span><span class="hljs-expression">{{<span class="hljs-variable">outer-helper</span> (<span class="hljs-variable">inner-helper</span> &apos;<span class="hljs-variable">abc</span>&apos;) &apos;<span class="hljs-variable">def</span>&apos;}}</span><span class="xml"></span>
</code></pre><p>&#x5728;&#x8FD9;&#x4E2A;&#x793A;&#x4F8B;&#x4E2D;&#xFF0C;<code>&apos;abc&apos;</code> &#x4F5C;&#x4E3A; <code>inner-helper</code> &#x7684;&#x53C2;&#x6570;&#x88AB;&#x8C03;&#x7528;&#xFF0C;&#x800C; <code>inner-helper</code> &#x7684;&#x8FD4;&#x56DE;&#x503C;&#x4F5C;&#x4E3A; <code>outer-helper</code> &#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x88AB;&#x8C03;&#x7528;&#xFF08;<code>&apos;def&apos;</code> &#x4F5C;&#x4E3A;&#x7B2C;&#x4E8C;&#x4E2A;&#x53C2;&#x6570;&#x88AB;&#x8C03;&#x7528;&#xFF09;&#x3002;</p>
<p><a href="demo/expressions-subexpressions.html" target="_blank">&#x793A;&#x4F8B;</a></p>
</div>
<!-- End .contents-->
<p><a name="expressions-whitespaccontrol-1.html" target="_blank"></a></p>
<h2>&#x63A7;&#x5236;&#x7A7A;&#x683C;</h2>
<!-- Start .contents-->
<dic class="contents">
<div class="bullet">
<p>&#x6A21;&#x677F;&#x7A7A;&#x767D;&#x53EF;&#x7531;&#x4EFB;&#x4F55; mustache &#x8BED;&#x6CD5;&#x62EC;&#x53F7;&#x4E2D;&#x4EFB;&#x610F;&#x4E00;&#x4FA7;&#x6DFB;&#x52A0; <code>~</code> &#x5B57;&#x7B26;&#x4EE5;&#x5220;&#x9664;&#x3002;When applied all whitespace on that side will be removed up to the first handlebars expression or non-whitespace character on that side.</p>
<pre><code class="lang-html"><span class="xml"></span><span class="hljs-expression">{{<span class="hljs-begin-block">#<span class="hljs-keyword">each</span> nav </span>~}}</span><span class="xml">
  <span class="hljs-tag">&lt;<span class="hljs-title">a</span> <span class="hljs-attribute">href</span>=<span class="hljs-value">&quot;</span></span></span><span class="hljs-expression">{{<span class="hljs-variable">url</span>}}</span><span class="xml"><span class="hljs-tag"><span class="hljs-value">&quot;</span>&gt;</span>
    </span><span class="hljs-expression">{{~<span class="hljs-begin-block">#<span class="hljs-keyword">if</span> test</span>}}</span><span class="xml">
      </span><span class="hljs-expression">{{~<span class="hljs-variable">title</span>}}</span><span class="xml">
    </span><span class="hljs-expression">{{~<span class="hljs-variable"><span class="hljs-keyword">else</span></span>~}}</span><span class="xml">
      Empty
    </span><span class="hljs-expression">{{~<span class="hljs-end-block">/<span class="hljs-keyword">if</span></span>~}}</span><span class="xml">
  <span class="hljs-tag">&lt;/<span class="hljs-title">a</span>&gt;</span>
</span><span class="hljs-expression">{{~<span class="hljs-end-block">/<span class="hljs-keyword">each</span></span>}}</span><span class="xml"></span>
</code></pre>
<p>&#x6570;&#x636E;&#x4E3A;&#xFF1A;</p>
<pre><code class="lang-js">{
  &quot;<span class="hljs-attribute">nav</span>&quot;: <span class="hljs-value">[
    {&quot;<span class="hljs-attribute">url</span>&quot;: <span class="hljs-value"><span class="hljs-string">&quot;foo&quot;</span></span>, &quot;<span class="hljs-attribute">test</span>&quot;: <span class="hljs-value"><span class="hljs-literal">true</span></span>, &quot;<span class="hljs-attribute">title</span>&quot;: <span class="hljs-value"><span class="hljs-string">&quot;bar&quot;</span></span>},
    {&quot;<span class="hljs-attribute">url</span>&quot;: <span class="hljs-value"><span class="hljs-string">&quot;bar&quot;</span></span>}
  ]
</span>}
</code></pre>
<p>&#x6700;&#x7EC8;&#x4F1A;&#x8F93;&#x51FA;&#x65E0;&#x6362;&#x884C;&#x548C;&#x7A7A;&#x683C;&#x7684;&#x5185;&#x5BB9;</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-title">a</span> <span class="hljs-attribute">href</span>=<span class="hljs-value">&quot;foo&quot;</span>&gt;</span>bar<span class="hljs-tag">&lt;/<span class="hljs-title">a</span>&gt;</span><span class="hljs-tag">&lt;<span class="hljs-title">a</span> <span class="hljs-attribute">href</span>=<span class="hljs-value">&quot;bar&quot;</span>&gt;</span>Empty<span class="hljs-tag">&lt;/<span class="hljs-title">a</span>&gt;</span>
</code></pre>
<p><a href="demo/expressions-whitespaccontrol-1.html" target="_blank">&#x793A;&#x4F8B;</a></p>
<p><a name="expressions-whitespaccontrol-2.html" target="_blank"></a></p>
<p>This expands the default behavior of stripping lines that are &quot;standalone&quot; helpers (only a block helper, comment, or partial and whitespace).</p>
<pre><code class="lang-html"><span class="xml"></span><span class="hljs-expression">{{<span class="hljs-begin-block">#<span class="hljs-keyword">each</span> nav</span>}}</span><span class="xml">
  <span class="hljs-tag">&lt;<span class="hljs-title">a</span> <span class="hljs-attribute">href</span>=<span class="hljs-value">&quot;</span></span></span><span class="hljs-expression">{{<span class="hljs-variable">url</span>}}</span><span class="xml"><span class="hljs-tag"><span class="hljs-value">&quot;</span>&gt;</span>
    </span><span class="hljs-expression">{{<span class="hljs-begin-block">#<span class="hljs-keyword">if</span> test</span>}}</span><span class="xml">
      </span><span class="hljs-expression">{{<span class="hljs-variable">title</span>}}</span><span class="xml">
    </span><span class="hljs-expression">{{<span class="hljs-variable"><span class="hljs-keyword">else</span></span>}}</span><span class="xml">
      Empty
    </span><span class="hljs-expression">{{<span class="hljs-end-block">/<span class="hljs-keyword">if</span></span>}}</span><span class="xml">
  <span class="hljs-tag">&lt;/<span class="hljs-title">a</span>&gt;</span>
</span><span class="hljs-expression">{{~<span class="hljs-end-block">/<span class="hljs-keyword">each</span></span>}}</span><span class="xml"></span>
</code></pre>
<p>&#x6E32;&#x67D3;&#x7ED3;&#x679C;&#xFF1A;</p>
<pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-title">a</span> <span class="hljs-attribute">href</span>=<span class="hljs-value">&quot;foo&quot;</span>&gt;</span>
    bar
<span class="hljs-tag">&lt;/<span class="hljs-title">a</span>&gt;</span>
<span class="hljs-tag">&lt;<span class="hljs-title">a</span> <span class="hljs-attribute">href</span>=<span class="hljs-value">&quot;bar&quot;</span>&gt;</span>
    Empty
<span class="hljs-tag">&lt;/<span class="hljs-title">a</span>&gt;</span>
</code></pre>
<p><a href="demo/expressions-whitespaccontrol-2.html" target="_blank">&#x793A;&#x4F8B;</a></p>
</div>
<!-- End .contents-->
<h2>Id Tracking</h2>
<!-- Start .contents-->
<dic class="contents">
<div class="bullet">
<p>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 <code>trackIds</code> compiler flag.</p>
<pre><code><span class="xml"></span><span class="hljs-expression">{{<span class="hljs-variable">foo</span> <span class="hljs-variable">bar.baz</span>}}</span><span class="xml"></span>
</code></pre><p>would call the helper <code>foo</code> with the value of <code>bar.baz</code> but also will include the literal string <code>&quot;bar.baz&quot;</code> in the <code>ids</code> field on the <code>options</code> argument.</p>
<p>This can be used for future lookup of parameters should it be necessary, but does add additional overhead.</p>
<p>When this mode is enabled, all builtin helpers will generate a <code><a href="reference.html#data-contextPath" target="_blank">@contextPath</a></code> variable that denotes the lookup path for the current context. It&apos;s highly recommended that generic helpers provide such a variable if they modify the context when executing their children.</p>
</div>
<!-- End .contents-->
        <a class="issue-tracker" href="https://github.com/nimojs/handlebarsjs.org/issues">
          发现翻译错误?告诉我们。
        </a>
      </div>
    </div>
    <div id="credits"></div>
    <a href="http://github.com/nimojs/handlebarsjs.org/" id="github">
      <img alt="Fork me on GitHub" src="static/images/forkme_right_darkblue_121621.png" />
    </a>
  </body>
</html>