[关闭]
@yacent 2016-07-11T17:09:28.000000Z 字数 4590 阅读 1343

JST (template.js) 学习笔记

网易|前端


官网:http://code.google.com/p/trimpath/wiki/JavaScriptTemplates

详细的教程,即翻译版本,可以直接看下面的博客地址:
http://bbs.chinaunix.net/thread-735901-1-1.html

模板一般放在一个display:none的textarea当中,然后待数据返回,绑定到相对应的部分,在js当中的处理是,使用 TrimPath.processDomTemplate(id, data)来将数据显示上去
具体的例子可以看上面详细教程当中的 code example


API


Syntax

Expressions and Expression 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}

Examples: 
${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}

一个表达式也可以像下面一样通过添加“%”字符来标识,这个可以避免在你的表达式中出现“}”时出错的情况

Expressions can also be optionally specified as "${% customer.firstName %}"

Statements (表达式)

Control Flow

{if testExpr} {elseif testExpr} {else} {/if}

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_index代表当前的 下标
__LIST__x 代表listExpr的结果

Examples:
{for x in customer.getRecentOrders()}
${x_index} : ${x.orderNumber} <br/>
{forelse}
You have no recent orders.
{/for}

Converted pseudo-code for the above...
var __LIST__x = customer.getRecentOrders();
if (__LIST__x != null && __LIST__x.length > 0) {
    for (var x_index in __LIST__x) {
        var x = __LIST__x[x_index];
        ${x_index} : {$x.orderNumber} <br/>
    }
} else {
    You have no recent orders.
}

Variable Declarations

{var varName}
|
{var varName = varInitExpr}

Macro Declarations

{macro macroName(arg1, arg2, ...argN)} ...body of the macro... {/macro}

A macro is like a JavaScript function, except the body of the macro is another JavaScript Template, not JavaScript.
    That is, the body of the macro may contain JST expressions and statements.

宏类似于一个javascript函数,不同点在于宏的主体是另外一个包含了诸如控制语句、循环语句的JST模板
  1. {macro htmlList(list, optionalListType)}
  2. {var listType = optionalListType != null ? optionalListType : "ul"}
  3. <$listType>
  4. {for item in list}
  5. ${item}
  6. {/for}
  7. </$listType>
  8. {/macro}

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']''...

  1. {macro userName(user)}
  2. {if user.aliasName != null && user.aliasName.length > 0} ${user.aliasName}
  3. {else}
  4. ${user.login}
  5. {/if}
  6. {/macro}
  7. ${exported.userName = userName |eat}

Cleverly, you might also set ''contextObject['exported'] = contextObject;'' It's circular, but it works.

--

Modifiers

The JavaScript Template engine includes several standard modifiers, which are used to modify the output of expressions. For example, the capitalize modifer is used like: ${customer.countryName|capitalize}

主要有以下的一些修饰器
capitalize
default:valueWhenNull
eat
escape
h   -> the same as the escape
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注