Velocity Introduction
Velocity is a Java-based template engine. It allows web designer to reference the methods defined in Java code. Web Designer can be programmed concurrently with Java engineers based on the MVC model. In other words, velocity separates Java development from web development.
Velocity can also generate web pages, SQL, PostScript, and other output based on the template, or integrate with other system components.
Velocity Template Language (VTL)
The VTL uses reference to embed dynamic content in Web site. Variables are a kind of reference. such as the following example
#set ($a = "Velocity")
This VTL statement, #set是指令, $a is a variable. Variables of string values are enclosed in single or double quotation marks, single quotation marks indicate raw string, double quotes, or the parsed value.
Rule of thumb: References begin with $ and is used to get something. Directives begin with # and is used to do something.
Reference
The VTL has three types of reference: variables, properties, and methods. As the designer of the VTL, you and your engineers must agree on reference's name. It's an agreement!
- variables, starting with #, and then identifier. Like $foo
- Properties, with a unique format. For example $customer. Name, which can be a value of key=name in map or an abbreviation for a method call
- method, similar to the properties format. For example $customer. getaddress or $customer. Address
Starting with velocity 1.6, all array reference are considered fixed-length lists. This means that you can use Java.util.List all methods on the array reference
Directive
Reference allows template designer to generate dynamic content in the Web sites. Directives--using script elements to manipulate the output of Java code
- The #set is used to assign values to variables, only reference types (variables, properties, and methods), literal type (string, number), ArrayList or map
- #if/#else/#elseif
- #foreach, Traverse
- #include, import local files in place, but the content is not rendered
- #parse, you can import not only vtl files, but also parse & render
- #evaluate, dynamic evaluate VTL. This allows, the template to evaluate a, string, that's created at render time
- #block, defining a VTL code block
- #macro, defining macros
- ...
Example
Recently encountered a case that requires a local custom template. This requires the template to render dynamically.
Javacode
public void Test1 () { Velocityengine Velo = new Velocityengine (); Velo.setproperty (Runtimeconstants.resource_loader, "classpath"); Velo.setproperty ("Classpath.resource.loader.class", ClasspathResourceLoader.class.getName ()); Velo.init ();
Velocitycontext context = new Velocitycontext (); map<string, string> alert = new hashmap<> (); Alert.put ("timestamp", "20160719"); Alert.put ("policy", "policy description");
String TPL = "<ul><li>cluster:${alert.timestamp}</li><li>colo:${alert.policy}</li> </ul> "; Context.put ("TPL", TPL); Context.put ("alert", alert);
Template Tmpl = velo.gettemplate ("ALERT_TEST.VM"); StringWriter writer = new StringWriter (); Tmpl.merge (context, writer); Log.info (Writer.tostring ()); } |
Alert_test.vm
Useful Links
- Velocity Chinese Primer
- Https://github.com/putaoshu/jdf/blob/master/doc/core_vm.md
- Http://www.cnblogs.com/yasin/archive/2010/04/02/1703188.html
- Velocity official website
- Http://velocity.apache.org/engine/1.7/user-guide.html#user-guide-contents
- Bosun Template
- https://golang.org/pkg/text/template/
- Bosun official website
- Https://bosun.org/quickstart
Velocity Local Custom Template