Java/Velocity/Macro

Материал из Java эксперт
Версия от 18:01, 31 мая 2010; (обсуждение)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Define and use Macro

       
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);
  }
}
-------------------------------------------------------------------------------------
#macro (writeTable $productList)
  #set ($rowCount = 1)      
  #foreach($product in $productList)      
  #if ($rowCount % 2 == 0)
    #set ($bgcolor = "#FFFFFF")
  #else
    #set ($bgcolor = "#CCCCCC")        
  #end
    <tr>
      <td bgcolor="$bgcolor">$product</td>
      <td bgcolor="$bgcolor">$product</td>
    </tr>            
    #set ($rowCount = $rowCount + 1)
  #end
#end
#set ($products = ["one", "two", "three"])
<html>
  <head>
    <title>Title</title>
  </head>
  <body>
    <table>
      #writeTable($products)
    </table>
  </body>
</html>





Use macro to wrap HTML tags

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 VMDemo {
  public static void main(String[] args) throws Exception {
    Velocity.init();
    Template t = Velocity.getTemplate("./src/demo.vm");
    VelocityContext ctx = new VelocityContext();
    ctx.put("var", new IteratorTool());
    Writer writer = new StringWriter();
    t.merge(ctx, writer);
    System.out.println(writer);
  }
}       
-------------------------------------------------------------------------------------
#macro( d )
<tr><td></td></tr>
#end
#d()





Velocity Macro With Parameters

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 VMDemo {
  public static void main(String[] args) throws Exception {
    Velocity.init();
    Template t = Velocity.getTemplate("./src/demo.vm");
    VelocityContext ctx = new VelocityContext();
    ctx.put("var", new IteratorTool());
    Writer writer = new StringWriter();
    t.merge(ctx, writer);
    System.out.println(writer);
  }
}
-------------------------------------------------------------------------------------
#macro( tablerows $color $somelist )
  #foreach( $something in $somelist )
      <tr><td bgcolor=$color>$something</td></tr>
  #end
#end

#set( $greatlakes = ["Superior","Michigan","Huron","Erie","Ontario"] )
#set( $color = "blue" )
<table>
    #tablerows( $color $greatlakes )
</table>