Java/Velocity/Variables
Версия от 18:01, 31 мая 2010; (обсуждение)
Содержание
Define and use variable in Velocity
import java.io.StringWriter;
import java.io.Writer;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.tools.generic.RenderTool;
public class VMDemo {
public static void main(String[] args) throws Exception {
Velocity.init();
Template t = Velocity.getTemplate("./src/VMDemo.vm");
VelocityContext ctx = new VelocityContext();
Writer writer = new StringWriter();
t.merge(ctx, writer);
System.out.println(writer);
}
}
-------------------------------------------------------------------------------------
#set ($a = 13)
#set ($b = 14)
#set ($c = $a * $b)
$c
Pass variable into Velocity
-------------------------------------------------------------------------------------
import java.io.StringWriter;
import java.io.Writer;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.tools.generic.RenderTool;
public class VMDemo {
public static void main(String[] args) throws Exception {
Velocity.init();
Template t = Velocity.getTemplate("./src/VMDemo.vm");
VelocityContext ctx = new VelocityContext();
ctx.put("myName","Joe");
Writer writer = new StringWriter();
t.merge(ctx, writer);
System.out.println(writer);
}
}
-------------------------------------------------------------------------------------
My name is $myName
Reference variable by name
import java.io.StringWriter;
import java.io.Writer;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.tools.generic.RenderTool;
public class VMDemo {
public static void main(String[] args) throws Exception {
Velocity.init();
Template t = Velocity.getTemplate("./src/VMDemo.vm");
VelocityContext ctx = new VelocityContext();
Writer writer = new StringWriter();
t.merge(ctx, writer);
System.out.println(writer);
}
}
-------------------------------------------------------------------------------------
#set ($a = "12.99")
#set ($b = "13.99")
$a = $a
$b = $b
Set variable value
import java.io.StringWriter;
import java.io.Writer;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.tools.generic.IteratorTool;
public class IteratorToolExample {
public static void main(String[] args) throws Exception {
Velocity.init();
Template t = Velocity.getTemplate("./src/iteratorTool.vm");
VelocityContext ctx = new VelocityContext();
ctx.put("var", new IteratorTool());
Writer writer = new StringWriter();
t.merge(ctx, writer);
System.out.println(writer);
}
}
-------------------------------------------------------------------------------------
#set($list = ["A", "B", "C", "D", "E"])
$list
Use a variable defined in Velocity
-------------------------------------------------------------------------------------
import java.io.StringWriter;
import java.io.Writer;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.tools.generic.RenderTool;
public class VMDemo {
public static void main(String[] args) throws Exception {
Velocity.init();
Template t = Velocity.getTemplate("./src/VMDemo.vm");
VelocityContext ctx = new VelocityContext();
Writer writer = new StringWriter();
t.merge(ctx, writer);
System.out.println(writer);
}
}
-------------------------------------------------------------------------------------
#set($msg = "Hi!")
I have a message: $msg
Velocity Variable Add
import java.io.StringWriter;
import java.io.Writer;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.tools.generic.RenderTool;
public class VMDemo {
public static void main(String[] args) throws Exception {
Velocity.init();
Template t = Velocity.getTemplate("./src/VMDemo.vm");
VelocityContext ctx = new VelocityContext();
Writer writer = new StringWriter();
t.merge(ctx, writer);
System.out.println(writer);
}
}
-------------------------------------------------------------------------------------
#set ($a = 13)
#set ($b = 14)
#set ($c = $a + $b)
$c
Velocity Variable Noise Reference
import java.io.StringWriter;
import java.io.Writer;
import java.util.Date;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
public class VMDemo {
public static void main(String[] args) throws Exception {
Velocity.init();
Template t = Velocity.getTemplate("./src/VMDemo.vm");
VelocityContext ctx = new VelocityContext();
ctx.put("aDate",new Date());
Writer writer = new StringWriter();
t.merge(ctx, writer);
System.out.println(writer);
}
}
-------------------------------------------------------------------------------------
Hello $aaDate.getDate()
Velocity Variable Quite Reference
import java.io.StringWriter;
import java.io.Writer;
import java.util.Date;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
public class VMDemo {
public static void main(String[] args) throws Exception {
Velocity.init();
Template t = Velocity.getTemplate("./src/VMDemo.vm");
VelocityContext ctx = new VelocityContext();
ctx.put("aDate",new Date());
Writer writer = new StringWriter();
t.merge(ctx, writer);
System.out.println(writer);
}
}
-------------------------------------------------------------------------------------
Hello $!aaDate.getDate()