Velocity below the Velocimacros settings

Source: Internet
Author: User

The #macro script element allows the template designer to define a reusable VTL template. Velocimacros is widely used in simple and complex ranks. The advent of velocimacros is to reduce coding and minimize typographical errors, providing an introduction to the concept of Velocimacros.

#macro (d)

<tr><td></td></tr>

#end

In the example above, Velocimacro is defined as D, and you can call it in any VTL directive as follows:

#d ()

When your template is called, Velocity will replace <tr><td></td></tr> with #d ().

Each velocimacro can have any number of parameters-even 0 parameters-although it is possible to set the number of parameters when defined, but the correct parameters must be specified when calling this velocimacro. Here is a velocimacro with two parameters, one parameter is color and the other parameter is array:

#macro (tablerows $color $somelist)

#foreach ($something in $somelist)

<TR><TD bgcolor= $color > $something </td></tr>

#end

#end

Velocimacro in the definition of this example, TableRows, has two elements. The first one is to replace the $color, and the second is to replace the $somelist.

Anything can be added to the VTL template via Velocimacro. TableRows Velocimacro is a foreach identity. There are two #end identified in the #tablerows velocimacro definition. The first one belongs to the #foreach and the second one belongs to the VELOCIMACRO definition.

#set ($greatlakes = [" Superior", "Michigan", "Huron", "Erie", "Ontario"])

#set ($color = "Blue")

<table>

#tablerows ($color $greatlakes)

</table>

Notice that the $greatlakes replaces the $somelist. When #tablerows Velocimacro is called, the following output is generated:

<table>

<TR><TD bgcolor= "Blue" >Superior</td></tr>

<TR><TD bgcolor= "Blue" >Michigan</td></tr>

<TR><TD bgcolor= "Blue" >Huron</td></tr>

<TR><TD bgcolor= "Blue" >Erie</td></tr>

<TR><TD bgcolor= "Blue" >Ontario</td></tr>

</table>

Velocimacros can implement inline definition within the velocity template, which means that other velocity templates within the same Web site cannot be Velocimacros defined. There are obvious benefits to defining a velocimacro that can be shared by all templates: It reduces the number of repetitions defined in a large stack of templates, saves working time, reduces the chance of errors, and guarantees a single point of modification.

The #tablerows ($color $list) Velocimacro defined above is defined in a Velocimacros template library (defined in velocity.properties), so this macro can be called in any canonical template. It can be applied multiple times and can be applied to different purposes. For example, the following call:

#set ($parts = ["Volva", "Stipe", "annulus", "Gills", "pileus"])

#set ($cellbgcol = "#CC00FF")

<table>

#tablerows ($cellbgcol $parts)

</table>

When a request is implemented for MUSHROOM.VM, Velocity will find the #tablerows velocimacro (defined in the Velocity.properties file) in the template Library, producing the following output:

<table>

<TR><TD bgcolor= "#CC00FF" >volva</td></tr>

<TR><TD bgcolor= "#CC00FF" >stipe</td></tr>

<TR><TD bgcolor= "#CC00FF" >annulus</td></tr>

<TR><TD bgcolor= "#CC00FF" >gills</td></tr>

<TR><TD bgcolor= "#CC00FF" >pileus</td></tr>

</table>

Velocimacro arguments

Velocimacro can use any of the following elements as parameters:

L Reference: Any Reference starting with $

L String literal:

L number literal:

L integerrange:[1....3] or [$foo .... $bar]

L Object array: ["A", "B", "C"]

L Boolean value: True, False

When passing a reference as a parameter to Velocimacro, be aware that reference is passed as a name as a parameter. This means that the value of the parameter is not generated until it executes within each velocimacro. This feature allows you to pass a method call as a parameter to Velocimacro, and each time the Velocimacro executes, it is executed by the method call that produces a different value. For example:

#macro (CallMe $a)

$a $a $a

#end

#callme ($foo. Bar ())

The result of the execution is that the bar () method of the reference $foo was executed three times.

In the first scan, there was an astonishing effect. But when you consider Velocimacro's most primitive motive: eliminate cut, paste, copy, and simply use the VTL to make it more flexible. It allows you to pass objects to velocimacro, such as a row object that produces a repeating sequence of colors, coloring a table.

If you do not need such a feature, you can do this by using the following methods:

#set ($myval = $foo. Bar ())

#callme ($myval)

Velocimacro Properties

A few lines in the Velocity.properties file can make the Velocimacros implementation more flexible. Note that more content can be seen in developer guide.

Velocimacro.libraary: A comma-delimited list of template libraries in the Velocity.properties file. By default, Velocity finds only one library: VM_GLOBAL_LIBRARY.VM. You can specify your own template library by configuring this property.

Velocimacro.permissions.allow.inline property in Velocity.properties file: There are two optional values of TRUE or FALSE, through which you can determine whether Velocimacros can be defined in the regular Within the template. The default value is ture―― allows designers to define velocimacros in their own templates.

The Velocimacro.permissions.allow.inline.replace.global property in the Velocity.properties file has two optional values, true and false, which allow the user to determine the inline VELOCIMACR o defines whether the global Velocimacro definition can be overridden (such as the Velocimacro defined within the file specified in the Velocimacro.library property). By default, this value is false. This prevents the local Velocimacro definition from overwriting the global definition.

The Velocimacro.permissions.allow.inline.local.scale property in the Velocity.properties file also has a true and false two optional value, which is false by default. Its purpose is to determine whether the Velocimacros defined by your inline is visible only within the template defined. In other words, if this property is set to True, an inline-defined velocimacros can only be used within the template that defines it. You can use this setting to implement a fantastic VM trick: a template can define a private implementation of the second VM that'll be called by the first VM WH En invoked by that template. All other templates is unaffected.

The Velocimacro.context.localscope property in the Velocity.properties file has true and false two optional values, and the default value is False. When set to true, any modification to the context through #set () within Velocimacro is considered a local setting for this velocimacro and does not permanently affect the content.

The Velocimacro.library.autoreload property in the Velocity.properties file controls the automatic loading of the Velocimacro library. The default is False. When set to Ture, a call to a VELOCIMACRO will automatically check whether the original library has changed, and if the change will reload it. This property allows you to reload the effect without restarting the servlet container, just as you would with a regular template. This property can be used only if the resource loader cache is the off state (File.resource.loader.cache = False). Note that this property is actually for development, not for the product.

Velocimacro Trivia

Velocimacro must be defined before they are used. In other words, your #macro () declaration should appear before you use Velocimacros.

It is especially important to note that if you try to #parse () a template that contains #macro (). Because #parse () occurs at run time, the parser determines in Parsetiem whether an element that looks like a VM element is a VM element, so that #parse ()-ing a set of VM claims will not work as expected. To get the expected results, you simply use Velocimacro.library to allow velocity to load your VMS at startup

Velocity below the Velocimacros settings

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.