Java/Velocity/If — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 10:07, 1 июня 2010
Содержание
If and elseif
<source lang="java">
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 ($companyName = "Name")
<html>
<head> <title>$companyName Homepage</title> </head> <body>
Welcome!!
#if ($userType == "Type 1")
You are Type 1!
#elseif ($userType == "Type 2")
You are an Type 2.
#elseif ($userType == "Type 3")
You are an Type 3!
#else
I don"t know what you are!
#end </body>
</html>
</source>
If Else and End
<source lang="java">
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 ($companyName = "Name")
<html>
<head> <title>$companyName Homepage</title> </head> <body>
Welcome!!
#if ($userType == "VIP")
You are a VIP!
#else
You are not a VIP!
#end </body>
</html>
</source>
If statement inside a for loop
<source lang="java">
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); }
}
<html>
<head> <title>Gimli"s Widgetarium</title> </head> <body>
$product | $product |
</body>
</html>
</source>
Use if in velocity
<source lang="java">
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"])
- foreach($item in $list)
#if($velocityCount <= 3) $item #end
- end
</source>