#macro script elements Allow template designers to define duplicate segments in the VTL template. VelocimaS is useful for complicated or simple occasions. Here's this Velocimracro, which is only used to save the key and reduce the typesetting error, and introduce some of the concept of Velocity macros.
#macro (d)
TD> TR>
#end
In the example, VelociMacro is defined as D, which can be called like the same form as other VTL instruction:
#d ()
When this template is called, Velocity replaces #d () with a single row of empty tables.
Velocimacro can take some parameters or without parameters (as shown in the example above). However, when he is called, the parameters belong must be the same as the parameters when it is defined. Many VelocimaS are defined as more than one parameter. The macro below has two parameters, a color, an array.
#macro (TableRows $ SOMELIST)
#foreach ($ Something In $ SOMELIST)
$ Something TD> TR>
#end
#end
VelociMacro defined in this example, named tableRows, requires two parameters. The first parameter replaces $ Color, the second instead of $ SOMELIST.
It can be written into the VTL template to write into the body part of VelocimaCro. The TABLEROWS macro is actually a Foreach statement. There are two #ende statements in the definition of #TABLEROWS macro, the first one belongs to #foreach, the second end macro definition.
Please note that $ Greatlakes replaces $ SOMELIST. In this way, when the #TABLEROWS macro is called, the following output is generated:
Superior td> tr>
Michigan TD> TR>
Huron TD> TR>
Erie TD> TR>
Ontario TD> TR>
table>
Velocimracros defines in the Velocity template statement, which means that it is not valid in other Velocity templates within the same site. Define a macro and share it with other templates: he reduces the work of repeated macros in a large number of templates, and reduces the opportunity to make an error, and ensure that all other macros have been effectively valid. .
But if #tableRows ($ color $ list) macro is defined in a VelocimaS template library, it can be used by other regular templates. Of course, it can be used for a variety of purposes and can be reused. In the Mushroom.vm template representing all fungi (Fungi), # tableROWS macros can be used to list typical mushrooms. #set ($ parts = ["Volva", "Stipe", "Annulus", "Gills", "Pileus"])
#set ($ Cellbgcol = "# cc00ff")
#TABLEROWS ($ Cellbgcol $ Parts)
table>
We perform requests for Mushroom.VM, and Velocity will find #TABLEROWS macros in the template library (defined in the velocity.properties file) and produce the following output:
Volva td> tr>
stipe td> tr>
Annulus TD> TR>
Gills TD> TR>
Pileus TD> TR>
table>
Velocimacro parameters
VelocimaS parameters can be the following VTL element:
Reference (Reference): Elements of '$'
String Litral: such as "$ foo" or 'Hello'
Field Number: 1, 2 ....
Integer range: [1..2] or [$ foo .. $ bar]
Object arrays: ["a", "b", "c"]
Boolean
Bur
When the reference is passed as a parameter to Velocimaos, please note that the reference is passed by "Name". This means that their value is generated in each of their VelocimaCro. This feature allows you to pass the reference in the method call, and perform method calls at each time. For example, fo, when calling the following VelociMacro,
#macro (Callme $ a)
$ a $ a $ a
#end
#callme ($ foo.bar ())
As a result, in the method bar (), the reference $ FOO was called 3 times.
When you look, this feature is surprising, and when you consider the original motivation of Velocimacros - Avoid a lot of "shear replication" operations in the VTL template - you will understand. It allows you to have stateless objects, such as repeating objects of color order in a color table, passed to Velocimacro.
If you need this feature, you can usually get a value from the method, as a new reference passed to the macro:
#set ($ myval = $ foo.bar ())
#callme ($ myVAL)
Velocimacro properties There are digital definitions in the Velocity.properties file that can be used to flexibly implement VelocimaS. For details, see Developer Guide.
Velocimracro.library - is a comma-separated list of all VelocimaCro template libraries. By default, Velocity searches for a single library VM_GLOBAL_LIBRARY.VM.. The pre-configured template path is used to find the VelociMacro library.
Velocimracro.Permissions.Allow.inline - This property determines if VelocimaS can be defined in a regular template, with a value of logical True or false. By default, set to TRUE, allowing designers to define macros in the production board.
Velocimracro.Permissions.Allow.inline.to.replace.global - Logic True or false allows you to indicate whether VelociMacro defined in the conventional template is allowed to define in the template library and loaded in the startup through the VelocimaCro.library property. The default is set to False.
Velocimaracro.Permissions.Allow.inline.local.scope - logical True or false, the default value is false. Controls whether Velocimracros defined in the template is only visible only in the template defined. In other words, if set to True, a template can define the macros that can only be used by him. You can use it to do some beautiful macros, if a global call another global macro, within the inline, when called by a template, the template can define a second for the first global macro. A private implementation of a global macro. All other templates are not affected.
VelociMacro.Context.localscope - Logic True or false, the default value is false. However, when set to TRUE, all modifications performed by #set () within VelocimaCro will be considered VelocimaCro, which will not affect its context.
VelocimaCro.Library.AutoreLoad - This property controls the automatic load of the VelociMacro library. The default is false. If set to true, the Velocimracro obtained by the called VelociMacro will be checked whether it is changed and is necessary to reload. This will enable you to change and test the VelociMacro library without having to restart the application server or servlet container, just like you work in a regular template. This model is only valid if it is closed when the resource loader is buffered (such as File.Resource.Loader.cache = FALSE). This feature is designed to be designed, do not use in production mode.
Velocimracro Trivia
Currently, VelocimaS must first define it first before it is used in the template. This means that #macro () claims that you should use VelocimaS before.
If you want #parse () a template containing the #macro () instruction, remember this is very important. Because #Parse () occurs at runtime, the parser is in parsing whether the template seems to be like a VM, so resolving a series of VM claims that it may not work very well. To avoid this, you can simply use VelocimaCro.library, allowing Velocity to load VM at startup.
2. Side escape VTL instruction
VTL can be escaped by a backslash ("/"), Directives Can Be Escaped with the Backslash Character in a manner Similar to Valid vtl References. ## #include ("a.txt") Renders AS
#include ("a.txt")
## / # DE ("a.txt") Renders as / # include ("a.txt")
/ # include ("a.txt")
## // # include ("a.txt") Renders as /
/ / # include ("a.txt")
Be careful when the escape instruction containing multiple scripting elements (such as F-ELSE-END statements) in a single instruction. Below is a typical VTL IF statement;
#if ($ JAZZ)
Vyacheslav Ganelin
#end
If $ jazz is true, the output is
Vyacheslav Ganelin
If $ jazz is false, there will be no output. The escape script element will change the output. Consider the following situation;
/ # ix ($ JAZZ)
Vyacheslav Ganelin
/ # end
Regardless of $ JAZZ is true or false, the output is
#if ($ JAZZ)
Vyacheslav Ganelin
#end
In fact, because all script elements are essential, $ JAZZ will never be evaluated. Set the backslash before the legally essential script element
/ / # ix ($ jazz)
Vyacheslav Ganelin
// # end
At this time, if $ jazz is true, the output is
/ Vyacheslav Ganelin
/
To understand this situation, please note that the end of a new line is to ignore a new line in the output. Therefore, after the '//' before #IF (), the # IF () block is followed by the first '/'. The last one / is located in the new line, because it is another new line after 'Ganelin', so the last one of the 位 # / / / 一 is part of the statement.
If $ jazz is false, there will be no output here. Note that the situation where the IF statement begins to destroy the IF statement will not be properly escaped:
/ / / # i ($ jazz)
Vyacheslave Ganelin
// # end
Here, # if is essential, but there is a #END being preserved; so a plurality of end statements will result in parsing errors.