Freemark--Template Engine

Freemark--What is Freemark?


​ FreeMark is a template engine, that is, a set of general tools that provide ==data model== and ==HTML template== to generate dynamic pages.

--request

​ --template + Freemark-dataService

​ --HTML

--response

​ It is a set of engine tools that Freemark returns to the user through the provided ==template==+==data model== dynamically generated HTML interface. It will dynamically generate different interface returns through templates deployed on the WEB server when WEB服务器responding to responseuser requests . requestThe data model here is often a java object.

Base Grammar--How to use freemark in program?


​ Freemark has its own basic grammar (generally similar to HTML), through which you can quickly generate different interfaces.

​ In daily development, as long as you master the simple Freemark syntax and a little HTML foundation, you can start development quickly.

​$ {...} : FreeMarker will output the real value to replace expressions inside curly braces, such expressions are called interpolation .

​Comments : Comments are similar to HTML comments, but they are marked with <#-- and -->. Unlike HTML comments , FTL comments do not appear in the output (not in the visitor's page) because FreeMarker skips them.

​FTL tags : Language tags for FreeMarker templates. FTL tags and HTML tags have some similarities, but they are FreeMarker directives and are not printed in the output. The names of these tags start with ==#==.

(User-defined FTL tags need to use @ instead of #)

Give a few commonly used instructions, and the rest of the instructions can be queried when needed for development

      <#if condition>...
      <#elseif condition2>...
      <#elseif condition3>...
      <#else>...
      </#if>
  <#include>...
  </#include>
     <#import "/lib/common.ftl" as com> 

  <#switch value> 
  <#case refValue>...<#break> 
  <#case refValue>...<#break> 
  <#default>... 
  </#switch> 
      <#list>...
      </#list>

Inner function--Advanced tutorials


​ Just like methods in java, built-in functions usually provide convenience for us to develop WEB programs. They are not something in the data model, they are added numerically by FreeMarker. To make it clear what part of the subvariables are, == uses ? instead of ==, . (dot) to access them.

  • [x]html 如果在 <#list animals as animal> ... </#list> animal?index给出了在animals中基于0开始的animal的索引值 animal?counter也像index,但是给出的是基于1的索引值 animal?item_parity基于当前计数的奇偶性,给出字符串“odd”或“even”。在给不同行着色时非常有用,比如: <td class="${animal?item_parity}Row">

     内建函数多种多样,并不一一列举,只举出几个常用的
    

cap_first: Capitalize the first letter of the string

lower_case: Convert the string to lowercase

upper_case: Convert the string to uppercase

trim: remove whitespace characters before and after the string

exists: determine whether the object is null

You can also directly ${mouse?if_exists})

${openingTime?date} ${openingTime?date_time} ${openingTime?time}

Output Boolean commonly used formatted date openingTime must be of Date type. For details, see the freemarker documentation Reference->build-in referece->build-in for date

​ For the rest of the development, you can just query it. It is worth mentioning that == null value==

There are often optional variables in the data model (sometimes they don't exist). Except for some human errors, FreeMarker cannot refer to non-existing variables unless it is explicitly told what to do when the variable does not exist. The following two typical processing methods are as follows:

  1. This part is for the programmer: a variable that does not exist and a variable that is null
  2. It's the same for FreeMarker, so the "missing" referred to here includes both cases.
  3. Wherever a variable is referenced, a default value can be specified to avoid the loss of the variable by following the variable name with a ==!== and ==defaultvalue==.
<h1>Welcome ${user!"visitor"}!</h1>

In this example, when user does not exist in the data model, the template will represent the value of user as the string "visitor". (When user exists, the template will show the value of ${user})

! Syntax for null value :

  1. variable!
  2. variable!defaultValue

The first usage does not specify a default value for the missing variable, indicating that the default value is an empty string, a set of length 0, or a Map object of length 0.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325976074&siteId=291194637
Recommended