Volicity grammar learning and summary

    Velocity is a java-based template engine (template engine). It allows anyone to simply use a template language to refer to objects defined by java code. When Velocity is applied to web development, interface designers and java program developers can simultaneously develop a web site that follows the MVC architecture, that is to say, page designers can only focus on the display effect of the page, while java program developers focus on business logical coding. Velocity separates the java code from the web page, which facilitates the long-term maintenance of the web site, and also provides us with an optional solution besides JSP and PHP.
     Velocity's capabilities go far beyond web site development, for example, it can generate SQL and PostScript, XML from templates, it can also be used as a standalone tool to generate source code and reports, or as an integrated component of other systems use. Velocity can also provide a template service for the Turbine web development framework. The way Velocity+Turbine provides a template service allows a web application to be developed in a true MVC model.

 

1. Basic grammar

1. "#" is used to identify Velocity script statements
including #set, #if, #else, #end, #foreach, #end, #include, #parse, #macro, etc.; for
    example:
    #if( $info.imgs)
        <img src="$info.imgs" border=0>
    #else
        <img src="noPhoto.jpg">
    #end

2. "$" is used to identify an object (or understood as a variable)
such as : $i, $msg, $TagUtil.options(...), etc.

3. "{}" is used to clearly identify the Velocity variable
. For example, in the page, there is a $someonename in the page. At this time, Velocity will use someonename as the variable name. If our program wants to display the name immediately after the variable of someone character, the above label should be changed to ${someone}name.

4. "!" is used to force non-existing variables to be displayed as blank.
For example, when the page contains $msg, if the msg object has a value, the value of msg will be displayed. If there is no msg object, the $msg character will be displayed on the page. This is not what we want. In order to display a non-existing variable or an object whose variable value is null, we only need to add a "!" sign before the variable name.
    For example: $!msg

 

2. Best Practices in EasyJWeb In

   theory, you can use all Velocity scripts and functions in the EasyjWeb template, but we do not recommend that you use too many complicated script expressions in the interface template. Add any complex logic to the interface template, let alone add variable declarations, logical operators, etc. to the interface template.
1. $!obj returns the object result directly.
    Such as: display the value of the java object msg in the html tag. <p$amp;>!msg</p>
  Displays the value of the msg object processed by the HtmlUtil object in the html tag

    <p$amp;>!HtmlUtil.doSomething($!msg)</p>

2. #if($!obj) #else #end judgment statements
  such as: In various open source applications of EasyJWeb, we often see the use of Example of pop-up prompt message msg.
   #if($msg)
       <script>
           alert('$!msg');
       </script>
   #end
    The script above means that when the object msg object exists, output the content behind <script> and so on.

3. #foreach( $info in $list) $info.someList #end
loops to read the objects in the set list and process them accordingly.
  For example: EasyJF open source forum system in the discussion (0.3) forum home page to display the html interface template script of popular topics:
           #foreach( $info in $hotList1)
                        <a /bbsdoc.ejf easyJWebCommand=show&&cid=$!info.cid"
                           target=" _blank"$amp;>!info.title</a$amp;>amp;$lt;br>


4、#macro(macroName)#end 脚本函数(宏)调用
不推荐在界面模板中大量使用。 Velocity中的宏我们可以理解为函数。
①宏的定义 #macro(宏的名称 $参数1 $参数2 …)   语句体(即函数体) #end
②宏的调用 #宏的名称($参数1 $参数2 …) 
说明:参数之间用空格隔开。
   如:在使用EasyJWeb Tools快速生成的添删改查示例中,可以点击列表的标题栏进行升降排序显示,这是我们在EasyJWeb应用中经常看到的一个排序状态显示的模板内容。
  函数(宏)定义,一般放在最前面
   #macro(orderPic $type)
       #if ($orderField.equals($type))
           <img src="${orderType}.gif">
       #end
   #end
    具体的调用如:<font color="#FFFFFF">头衔#orderPic("title")</font>

5、包含文件#inclue("模板文件名")或#parse("模板文件名")
  主要用于处理具有相同内容的页面,比如每个网站的顶部或尾部内容。
  如:#parse("/blog/top.html")或#include("/blog/top.html")
  区别:
       1   若包含的文件中有Velocity脚本标签,将会进一步解析,而include将原样
            显示。
       2    #parse只能指定单个对象。而#include可以有多个
             示范代码:
               #include ("one.gif", "two.txt", "three.htm" )
               #parse ("parsefoo.vm")

 

三、关于#set的使用

  在万不得已的时候,不要在页面视图自己声明Velocity脚本变量,也就是尽量少使用#set。有时候我们需要在页面中显示序号,而程序对象中又没有包含这个序号属性同,可以自己定义。如在一个循环体系中,如下所示:
  #set ($i=0)
  #foreach($info in $list)
  序号:$i
  #set($i=$i+1)
  #end

 

四、Velocity脚本语法摘要

1.声明:#set ($var=XXX)
变量引用,字面字符串,属性引用,方法引用,字面数字,数组列表。
#set( $monkey = $bill ) ## variable reference
#set( $monkey.Friend = "monica" ) ## string
#set( $monkey.Blame = $whitehouse.Leak ) ## property reference
#set( $monkey.Plan = $spindoctor.weave($web) ) ## method reference
#set( $monkey.Number = 123 ) ##number
#set( $monkey.Say = ["Not", $my, "fault"] ) ## ArrayList  
算术运算符
#set ( $foo = $bar + 3 )    #set ( $foo = $bar - 4 )    #set ( $foo = $bar * 6 )    #set ( $foo = $bar / 2 )

2、注释:
  单行## XXX
  多行#* xxx
  xxxx
  xxxxxxxxxxxx*#

  References 引用的类型


3、变量 Variables
以 "$" 开头,第一个字符必须为字母。character followed by a VTL Identifier. (a .. z or A .. Z).
  变量可以包含的字符有以下内容:
  alphabetic (a .. z, A .. Z)
  numeric (0 .. 9)
  hyphen ("-")
  underscore ("_")

4、Properties
  $Identifier.Identifier
  $user.name
  hashtable user中的的name值.类似:user.get("name")

5、Methods
  object user.getName() = $user.getName()

6、Formal Reference Notation
  用{}把变量名跟字符串分开

  如
  #set ($user="csy"}
  ${user}name
  返回csyname

  $username
  $!username
  $与$!的区别
  当找不到username的时候,$username返回字符串"$username",而$!username返回空字符串""

7、双引号 与 引号
  #set ($var="helo")
  test"$var" 返回testhello
  test'$var' 返回test'$var'
  可以通过设置 stringliterals.interpolate=false改变默认处理方式

8、条件语句
  #if( $foo )
   <strong>Velocity!</strong>
  #end
  #if($foo)
  #elseif()
  #else
  #end
  当$foo为null或为Boolean对象的false值执行.

9、逻辑运算符:== && || !

10、循环语句
#foreach($var in $arrays ) 
// 集合包含下面三:Vector, a Hashtable or an Array
#end

#foreach( $product in $allProducts )
  <li$amp;>product</li>
#end

#foreach( $key in $allProducts.keySet() )
  <li>Key: $key -> Value: $allProducts.get($key)</li>
#end

#foreach( $customer in $customerList )
  <tr$amp;>amp;$lt;td$amp;>velocityCount</td$amp;>amp;$lt;td$amp;>customer.Name</td$amp;>amp;$lt;/tr>
#end
语句的嵌套
    #foreach ($element in $list)  ## inner foreach 内循环
#foreach ($element in $list) This is $element. $velocityCount <br>inner<br$amp;>amp;$nbsp;      #end        ## inner foreach 内循环结束 
        ## outer foreach     This is $element. 
$velocityCount <br>outer<br>
#end

11、velocityCount变量在配置文件中定义
  # Default name of the loop counter
  # variable reference.
  directivunter.name = velocityCount
  # Default starting value of the loop
  # counter variable reference.
  directivunter.initial.value = 1

12、包含文件
  #include( "one.gif","two.txt","three.htm" )

13、Parse导入脚本
  #parse("me.vm" )

14、#stop 停止执行并返回
      停止执行模板引擎并返回,把它应用于debug是很有帮助的。

15、定义宏Velocimacros ,相当于函数支持包含功能
  #macro( d )
   <tr$amp;>amp;$lt;td$amp;>amp;$lt;/td$amp;>amp;$lt;/tr>
  #end
  调用
  #d()

16、带参数的宏
  #macro( tablerows $color $somelist )
  #foreach( $something in $somelist )
   <tr$amp;>amp;$lt;td bgcolor=$color$amp;>something</td$amp;>amp;$lt;/tr>
  #end
  #end

17、Range Operator
#foreach( $foo in [1..5] )

18、转义字符
   如果reference被定义,两个’\’意味着输出一个’\’,如果未被定义,刚按原样输出。    #set($email = "foo" ) 
    $email    
  \$email     
\\$email      
\\\$email
输出:       foo       $email....

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326996200&siteId=291194637