Java/Velocity/If

Материал из Java эксперт
Перейти к: навигация, поиск

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);
 }

}


  1. 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);
 }

}


  1. 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>
#set ($rowCount = 1) #set ($products = ["one", "two", "three"]) #foreach($product in $products) #if ($rowCount % 2 == 0) #set ($bgcolor = "#FFFFFF") #else #set ($bgcolor = "#CCCCCC") #end #set ($rowCount = $rowCount + 1) #end
$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);
 }

}


  1. set($list = ["A", "B", "C", "D", "E"])
  2. foreach($item in $list)
   #if($velocityCount <= 3)
       $item
   #end
  1. end
      </source>