freemarker commonly used method

FreeMarker is a  template engine : that is, a template-based and data to be changed, and used to generate output text (HTML web pages, e-mail, configuration files, source code, etc.) of common tools.

 

 

 Organize some of the commonly used methods

Data traversal

List traversal methods provided by freemarker, Users traverse the object is to return after the user traversal value

假设 users 包含 ['Joe', 'Kate', 'Fred'] 序列:

<#list users as user>
  <p>${user}
</#list>

<#list>必须要按照这样的格式去书写

If you want to limit the value of, for example, before it is to take five users [0..4], and arrays, are zero-based

Analyzing conditions  

There is also provided freemarker and judgment of native js conditions are similar to judge if else by

<#if condition>
  ...
<#elseif condition2>
  ...
<#elseif condition3>
  ...
...
<#else>
  ...
</ #If > 
conditioncondition2, and the like: to be calculated as Boolean values.

Creating Variables

freemarker for the convenience store a variable, but also gave us a way to provide variable,

 

<#Assign name1 = value1 name2 = value2 ... nameN valueN =>

 

name: Variable name. It is not an expression. And it can be written as a string, if the variable names contain reserved characters This is useful, for example  <#assign "foo-bar" = 1>. value is the value;

 

Case

By implementing to a, so that you understand the method of use

<#if ( (Hotwz??) && (Hotwz?size>0))>
                <#list Hotwz[0..2] as HotwzList>
                    <#list HotwzList.coverImagesList[0..0] as Attach>
                        <#if ( (Attach??) && (Attach?size> 0))>
                            <a href="https://dxshare.dianzhenkeji.com/recommend/article/1/${HotwzList.articleId}"
                                class="recommand_list">
                                <div class="news_desc">
                                    <div class="title"> ${HotwzList.contentTitle} </div>
                                    <div class="desc"> <span>${HotwzList.articleOrigin} |</span>
                                        <span>${HotwzList.publishTime?date}</span> </div>
                                </div>
                                <div class="news_img"> <img src="${Attach.url}" alt="">
                                </div>
                            </a>

                        </#if>
                    </#list>

                </#list>
                <#else>
            </#if>

Wherein Hotwz is to traverse the object, determining if conditions then take the first three, (the Attach ??) && (the Attach? Size> 0)  is used to determine whether the value is greater than zero length, two conditions are met, it execute $ {HotwzList.contentTitle}, then that value must be $ {} this format value , contentTitle own field names defined in the background

These are commonly used in Web pages the way, if you want to know more, you can take the reference http://freemarker.foofun.cn/toc.html official website

Published 76 original articles · won praise 71 · views 1842

Guess you like

Origin blog.csdn.net/Govern66/article/details/104290223