??xml version="1.0" encoding="utf-8" standalone="yes"?>
<ww:param name="name" value="'test'" /><!--注意单引?->
<ww:param name="colCnt" value="4" />
<ww:param name="list" value="{'1','2','3','4','5','6'}" />
</ww:component>
二:l于理解freemaker的多值类型?br />
<#if (parameters.list?size%parameters.colCnt)?int==0>Q求余数是否?Q必d上内|方法int?br />
freemaker内置了chunk分块的方法,但是我始l没有想到怎么这个快按纵向进行排列。横向好_直接按行输出。因此写下列模板代码来实现?br />
<#assign itemCount = 0/>
<#if parameters.list??>
<#assign width=(100/parameters.colCnt)?int>
<#assign itemCount = itemCount + 1/>
<#assign rowCnt=0/>
<#assign colCnt=0/>
<#list parameters.list?chunk(parameters.colCnt) as row>
<#assign rowCnt=rowCnt+1/>
<tr>
<#list row as cell>
<#assign itemCount = itemCount + 1/>
<#assign colCnt=colCnt+1/>
<#if rowCnt==0 && col<parameters.colCnt-1 >
<td width='${width}%'>
<#else>
<td>
</#if>
<#if parameters.listKey??>
<#assign itemKey = cell[parameters.listKey]/>
<#else>
<#assign itemKey = cell/>
</#if>
<#if parameters.listValue??>
<#assign itemValue = cell[parameters.listValue]/>
<#else>
<#assign itemValue = cell/>
</#if>
<#assign itemKeyStr=itemKey.toString() />
<input type="checkbox" name="${parameters.name?html}" value="${itemKeyStr?html}" id="${parameters.name?html}-${itemCount}"<#rt/>
<#if tag.contains(parameters.nameValue, itemKey)>
checked="checked"<#rt/>
</#if>
<#if parameters.disabled!(false)>
disabled="disabled"<#rt/>
</#if>
/>
<label for="${parameters.name?html}-${itemCount}" class="checkboxLabel">${itemValue?html}</label>
</td>
</#list>
</#list>
<#else>
</#if>
jsp引用面试代码Q?br /><table width="80%">
<ww:component template="checkboxlist_landscapeCols.ftl">
<ww:param name="name" value="'test'" />
<ww:param name="colCnt" value="4" />
<ww:param name="list" value="{'1','2','3','4','5','6'}" />
</ww:component>
</table>
<#list 0..rowCnt-1 as row>
<tr>
<#list 0..parameters.colCnt?int-1 as col>
<#assign itemCount = itemCount + 1/>
<#if row==0 && col<parameters.colCnt-1 >
<td width='${width}%'>
<#else>
<td>
</#if>
<#if row+col*rowCnt<parameters.list?size >
<#assign item=parameters.list[row+col*rowCnt] />
<#if parameters.listKey??>
<#assign itemKey = item[parameters.listKey]/>
<#else>
<#assign itemKey = item/>
</#if>
<#if parameters.listValue??>
<#assign itemValue = item[parameters.listValue]/>
<#else>
<#assign itemValue = item/>
</#if>
<#assign itemKeyStr=itemKey.toString() />
<input type="checkbox" name="${parameters.name?html}" value="${itemKeyStr?html}" id="${parameters.name?html}-${itemCount}"<#rt/>
<#if tag.contains(parameters.nameValue, itemKey)>
checked="checked"<#rt/>
</#if>
<#if parameters.disabled!(false)>
disabled="disabled"<#rt/>
</#if>
/>
<label for="${parameters.name?html}-${itemCount}" class="checkboxLabel">${itemValue?html}</label>
<#else>
</#if>
</td>
</#list>
</tr>
</#list>
<#else>
</#if>
jsp引用面试代码Q?br /><table width="80%">
<ww:component template="checkboxlist_portraitCols.ftl">
<ww:param name="name" value="'test'" />
<ww:param name="colCnt" value="4" />
<ww:param name="list" value="{'1','2','3','4','5','6'}" />
</ww:component>
</table>
<#escape x as x?html>
First name: ${firstName}
Last name: ${lastName}
Maiden name: ${maidenName}
</#escape>
{同于:
<#assign name=value> or <#assign name1=value1name2=value2... nameN=valueN> or <#assign same as above... in namespacehash> or <#assign name> capture this </#assign> or <#assign name in namespacehash> capture this </#assign>十:global指o
创徏或者替换一个已l存在的变量Q只能作用于层变量? 不好的做法:<#assign x>Hello ${user}!</#assign>
更改为:<#assign x="Hello ${user}!">
<@macro_or_transfparam1=val1param2=val2...paramN=valN/><@macro_or_transfparam1=val1param2=val2...paramN=valN ; lv1, lv2, ..., lvN/><@macro_or_transf...> ... </@macro_or_transf>十四Qmacro,nested,return
<@macro_or_transf...> ... </@><@macro_or_transfval1, val2, ..., valN/>
~省? name!"unknown" 或?(user.name)!"unknown" 或?name! 或?(user.name)!
null值检? name?? or (user.name)??
转义列表Q?br />
Escape sequence | Meaning |
---|---|
\" | Quotation mark (u0022) |
\' | Apostrophe (a.k.a. apostrophe-quote) (u0027) |
\\ | Back slash (u005C) |
\n | Line feed (u000A) |
\r | Carriage return (u000D) |
\t | Horizontal tabulation (a.k.a. tab) (u0009) |
\b | Backspace (u0008) |
\f | Form feed (u000C) |
\l | Less-than sign: < |
\g | Greater-than sign: > |
\a | Ampersand: & |
\{ | Curly bracket: { |
\xCode | Character given with its hexadecimal Unicode code (UCS code) |
Ҏ变量是指freemaker引擎本n定义的变量。访问时Q以.variable_name的语法访问?br />
变量表达式支持嵌套模式,比如Q?{"Hello ${user}!"}?br />
变量表达式在指o中的使用情况Q?br />变量表达式可以在指o中,用“”的方式存在Q不如:<#include "/footer/${company}.html">.
但是不允怸面的方式存在Q?<#if ${isBig}>Wow!</#if>, 正确写法是:<#if isBig>Wow!</#if>.
而且 <#if "${isBig}">Wow!</#if>写法也不正确Q因?${isBig}"q回的是字符Ԍ不是booleancd?br />
字符串中取字W或字符串采用以下语法:${user[0]},${user[0..2]} ${user[4..]},${user?string(4)}
序列操作Q?br /> 加法Q?lt;#list ["Joe", "Fred"] + ["Julia", "Kate"] as user> 但要注意串联之后的读取速度变慢?br /> 子序列:seq[1..4]
序列和hash的串联都只能用于两个相加Q不能有多个相加的模式,hash相加Q如果两个相加的hash存在相同的keyQ则后面会覆盖前面的?br />
在?gt;=或?gt;Ӟ需要注意一些问题,因ؓfreemaker会将>解释成标记的关闭W,Z解决q个问题Q需要在表达式加上括P比如Q?<#if (x > y)>. 或者?> and <W号来代ѝ?/tt>
无值变量(包括无该变量QnullQ返回voidQ无属性等Q:unsafe_expr!default_expr or unsafe_expr! or (unsafe_expr)!default_expr or (unsafe_expr)!
~省值可以是McdQ不一定是数字Q比如:hits!0 或?colors!["red", "green", "blue"].
如果~省值忽略,那么会默认为空丌Ӏ空序列或者空hashQ因为freemarker支持多类型的倹{不q要让默认gؓ0或falseQ则不能省略~省倹{?br />
非顶层变量的无值处理:
product.color!"red"Q只处理product不ؓI,color为空的缺省值处理,如果product为空Q则freemaker会抛出异常?product.color)!"red"Q则会处理product为空Qcolor为空Q或者没有color属性的无值情c?br />
无值变量的判断操作Q?em>unsafe_expr?? or (unsafe_expr)??
<#macro repeat count>
<#list 1..count as x>
<#nested x, x/2, x==count>
</#list>
</#macro>
<@repeat count=4 ; c, halfc, last>
${c}. ${halfc}<#if last> Last!</#if>
</@repeat>
${openingTime?string.short} ${openingTime?string.medium} ${openingTime?string.long}l合使用Q?{lastUpdated?string.short_long}
Built-in | Returns true if the value is a ... |
---|---|
is_string | string |
is_number | number |
is_boolean | boolean |
is_date | date (all types: date-only, time-only and date-time) |
is_method | method |
is_transform | transform |
is_macro | macro |
is_hash | hash |
is_hash_ex | extended hash (i.e. supports ?keys and ?values) |
is_sequence | sequence |
is_collection | collection |
is_enumerable | sequence or collection |
is_indexable | sequence |
is_directive | macro or transform |
is_node | node |
number
replace
rtf
\ replaced with \\
{ replaced with \{
} replaced with \}
starts_with
trimQupper_case。word_listQxml
字符串内|方法可以接受的通用标记Q?br /> i: 大小写不敏感?br /> f: 只用于第一个,常用于替换,查找{,取第一ơ出?br /> r: 子串是个正则表达式?br /> m: 正则表达式的多行模式?br /> s: 单行模式?br /> c: 允许I白或注释在正则表达式中?br /><#assign s = 'foo bAr baar'>
${s?replace('ba', 'XY')}
i: ${s?replace('ba', 'XY', 'i')}
if: ${s?replace('ba', 'XY', 'if')}
r: ${s?replace('ba*', 'XY', 'r')}
ri: ${s?replace('ba*', 'XY', 'ri')}
rif: ${s?replace('ba*', 'XY', 'rif')}
Built-in |
i |
r |
m |
s |
c |
f |
---|---|---|---|---|---|---|
replace |
Yes |
Yes |
Only with r |
Only with r |
Only with r |
Yes |
split |
Yes |
Yes |
Only with r |
Only with r |
Only with r |
No |
match |
Yes |
No |
Yes |
Yes |
Yes |
No |
Template
Type
Model
6Q方法 ?br />public class IndexOfMethod implements TemplateMethodModel {
public TemplateModel exec(List args) throws TemplateModelException {
if (args.size() != 2) {
throw new TemplateModelException("Wrong arguments");
}
return new SimpleNumber(
((String) args.get(1)).indexOf((String) args.get(0)));
}
}
root.put("indexOf", new IndexOfMethod());
<#assign x = "something">
${indexOf("met", x)}
${indexOf("foo", x)}
name | class |
---|---|
capture_output | freemarker.template.utility.CaptureOutput |
compress | freemarker.template.utility.StandardCompress |
html_escape | freemarker.template.utility.HtmlEscape |
normalize_newlines | freemarker.template.utility.NormalizeNewlines |
xml_escape | freemarker.template.utility.XmlEscape |