- expressions and expressions modifiers 表达式和表达式修改器
${expr}
${expr|modifier}
${expr|modifier1|modifier2|...|modifierN}
${expr|modifier1:argExpr1_1}
${expr|modifier1:argExpr1_1,argExpr1_2,...,argExpr1_N}
${expr|modifier1:argExpr1_1,argExpr1_2|...|modifierN:argExprN_1,argExprN_2,...,argExprN_M}
- expr是合法的javascript表达式,’}'除外
- modifier的格式 modifierName[:argExpr1[,argExpr2[,argExprN]]]
示例:
${customer.firstName}
${customer.firstName|capitalize}
${customer.firstName|default:"no name"|capitalize}
${article.getCreationDate()|default:new Date()|toCalenderControl:"YYYY.MM.DD",true,"Creation Date"}
${(lastQuarter.calcRevenue() - fixedCosts) / 1000000}
如果expression里含有'{','}'字符,不能再使用${expression}了,可以使用${%expression%}
2. control flow 控制流
{if testExpr}
{elseif testExpr}
{else}
{/if}
- testExpr是合法的js表达式,’}'除外
- testExpr不需要括号括起来
示例:
{if customer != null && customer.balance > 1000}
We love you!
{/if}
{if user.karma > 100}
Welcome to the Black Sun.
{elseif user.isHero}
Sir, yes sir! Welcome!
{if user.lastName == "Yen"}
Fancy some apple pie, sir?
{/if}
{/if}
<a href="/login{if returnURL != null && returnURL != 'main'}?goto=${returnURL}{/if}">Login</a>
3. Loops 循环
{for varName in listExpr}
{/for}
{for varName in listExpr}
...main body of the loop...
{forelse}
...body when listExpr is null or listExpr.length is 0...
{/for}
- varName是合法的js变量名
- listExpr必须是js的数组或Object或null,The listExpr is evaluated only once.
- 在循环体内可以使用varName_index来索引
示例:
{for x in customer.getRecentOrders()}
${x_index} : ${x.orderNumber} <br/>
{forelse}
You have no recent orders.
{/for}
4. variable declarations 变量声明
{var varName}
{var varName = varInitExpr}
- varName是合法的js变量名
- varInitExpr不能含有’}'字符
示例:
{var temp = crypto.generateRandomPrime(4096)}
Your prime is ${temp}.
5. macro declarations 宏定义
{macro macroName(arg1, arg2, ...argN)}
...body of the macro...
{/macro}
- 宏相当于js的函数,不同的是宏的体内是符合jst语法的语句和表达式,而不是js语句
- macroName是任何合法的js变量名
- 返回值类型是string
- 通过${macroName()}这种方式调用
示例:
{macro htmlList(list, optionalListType)}
{var listType = optionalListType != null ? optionalListType : "ul"}
<${listType}>
{for item in list}
<li>${item}</li>
{/for}
</${listType}>
{/macro}
Using the macro...
${htmlList([ 1, 2, 3])}
${htmlList([ "Purple State", "Blue State", "Red State" ], "ol")}
{var saved = htmlList([ 100, 200, 300 ])}
${saved} and ${saved}
关于宏的作用域:
Regarding macro scope: by default, macros are defined private to each template. If you want to
export a macro so that it can be reused in other templates (such as building up a helper
library of macros), one approach is to save a reference to your macro into your
''contextObject''. For example, in the ''contextObject'' that's the argument to
''template.process(contextObject)'', you can set ''contextObject['exported'] = {};''
before you call process(). Then, here's how you can capture a macro into
''contextObject['exported']''...
{macro userName(user)}
{if user.aliasName != null && user.aliasName.length > 0}
${user.aliasName}
{else}
${user.login}
{/if}
{/macro}
${exported.userName = userName |eat}
Cleverly, you might also set ''contextObject['exported'] = contextObject;'' It's circular,
but it works.
6. CDATA text section
{cdata}
...text emitted without JST processing...
{/cdata}
{cdata EOF}
...text emitted without JST processing...
EOF
被包含的text不会被jst处理,This can be useful if you're using a JavaScript Template to
generate a JavaScript Template.
- EOF可以是任意的标识符,当然不能包含’}',用来标识text的结尾
- ‘…’可以包含任何东西,如果有换行,jst将原封不动的返回一个换行
示例:
Hello, ${user.firstName}.
An example of expression markup in JST looks like…
{cdata END_OF_THE_CDATA_SECTION}
${customer.firstName} ${customer.lastName}
END_OF_THE_CDATA_SECTION
…which shows a customer‘s name.
Let me repeat that…
{cdata}
${customer.firstName} ${customer.lastName}
{/cdata}
…will show a customer’s name.
输出结果是:
Hello, Steve.
An example of expression markup in JST looks like…
${customer.firstName} ${customer.lastName}
…which shows a customer‘s name.
Let me repeat that…
${customer.firstName} ${customer.lastName}
…will show a customer’s name.
7 in-line javascript 内嵌js
(1) eval block
{eval}
...javascript evaluated during JST processing...
{/eval
{eval EOF}
...javascript evaluated during JST processing...
EOF
- EOF的要求同上
- The {eval} markup block can be useful to define multi-line JavaScript event handler functions near where they are used.
示例:
<select onchange="sel_onchange()"></select>
{eval}
sel_onchange = function() {
...Do some complicated javascript...;
...more js code here...;
}
{/eval}
请注意,这里没有在sel_onchange上使用'var'关键词,主要是确保sel_onchange处于全局域,hence
usable as an event handler function.event handler function 一定要全局域吗?待考。
(2)minify block
{minify} …multi-line text which will be stripped of line-breaks during JST processing… {/minify {minify EOF} …multi-line text which will be stripped of line-breaks during JST processing… EOF
- EOF要求同上
- A {minify} block allows you to inline long JavaScript or CSS code into your HTML attributes. For JavaScript, this is especially useful for event handlers like onchange, onmousedown, onfocus, onblur, etc. Without {minify}, handling linebreaks or newlines in long JavaScript code is possible but unwieldy.
示例:
<select onchange="{minify}
...Do some complicated multi-line javascript...;
...more js code here...;
this.enabled = false;
{/minify}">
<select onchange="{minify END_OF_JS}
...Do some complicated multi-line javascript...;
...more js code here...;
this.enabled = false;
END_OF_JS">
The {minify} block is also useful to make long inline CSS <style> attributes readable and maintainable, which can sometimes be useful in Internet Explorer which does not seem to support dynamically generated <style> tags.
<div id=“commentPanel”
style=“{minify}
display:none;
margin: 1em;
border: 1px solid #333;
background: #eee;
padding: 1em;
{/minify}“>
…
</div>
辅助函数:defined(str)
示例:
{if defined('adminMessage')}
System Administrator Important NOTICE: ${adminMessage}
{/if}