Java/Velocity/HTML — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 10:07, 1 июня 2010
Содержание
Use Velocity to generate HTML based email
<source lang="java">
import java.io.StringWriter; import java.util.List; import java.util.ArrayList; import java.util.Map; import java.util.HashMap; import org.apache.velocity.Template; import org.apache.velocity.VelocityContext; import org.apache.velocity.app.VelocityEngine; public class EmailDemo {
public static void main( String[] args ) throws Exception { /* * first, get and initialize an engine */ VelocityEngine ve = new VelocityEngine(); ve.init(); /* * organize our data */ ArrayList list = new ArrayList(); Map map = new HashMap(); map.put("name", "Cow"); map.put("price", "$100.00"); list.add( map ); map = new HashMap(); map.put("name", "Eagle"); map.put("price", "$59.99"); list.add( map ); map = new HashMap(); map.put("name", "Shark"); map.put("price", "$3.99"); list.add( map ); /* * add that list to a VelocityContext */ VelocityContext context = new VelocityContext(); context.put("petList", list); /* * get the Template */ Template t = ve.getTemplate( "./src/email_html.vm" ); /* * now render the template into a Writer, here * a StringWriter */ StringWriter writer = new StringWriter(); t.merge( context, writer ); /* * use the output in the body of your emails */ System.out.println( writer.toString() ); }
}
<HTML> <HEAD> <TITLE>Pet Store Sale!</TITLE> </HEAD>
<BODY>
$petList.size() Pets on Sale!
This is an email generated by velocity
This month only, choose from : #set( $count = 1 )
$count) | $pet.name | $pet.price |
Call Today! Bests
www.jexp.ru
</BODY> </HTML> </source>
Use Velocity to generate HTML document
<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>
Velocity Generate Document: anakia task
Velocity works With HTML
<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>