|
Re: Formatting Issue (Whitespace): msg#00195jakarta.velocity.user
Well, I don't think this is a problem, IMO. The rule is simple: - Directives begins with # and ends with the line separator (\n). - Everything except directives and references are raw texts (including line separators). Yes, I agree that it is not easy to read templates without indentations of directives. However, if Velocity must recognize indentations of directives, it will be much harder to implement the parser. And with the implementation, it will be confusing when you must have extra spaces. For example, ----------------------------------------------------------- #foreach($item in $list) #if($someCondition) some text and new line and two spaces. #end #end ----------------------------------------------------------- Note two extra spaces before #end of #if. So, I think that the current treating of whitespaces are OK. Just my 2 yen :-) Here is the correct template of a simplified version (tested on velocity-1.3-rc2). ----------------------------------------------------------- #macro(button $index) #if($menu.size() > $index) <A HREF="$menu.get($index).get("link")"><IMG SRC="$menu.get($index).get("imgsrc")"></A>#else <!-- empty button -->#end #end #macro(imenu $startindex) #if($menu.size() > $startindex) <TABLE CELLSPACING="0" CELLPADDING="0" WIDTH="600" BORDER="0"> <TR> <TD>#button($startindex)</TD> #set($startindex = $startindex + 1) <TD>#button($startindex)</TD> #set($startindex = $startindex + 1) <TD>#button($startindex)</TD> #set($startindex = $startindex + 1) <TD>#button($startindex)</TD> #set($startindex = $startindex + 1) <TD>#button($startindex)</TD> #set($startindex = $startindex + 1) </TR> </TABLE> #end #end #imenu(0) ----------------------------------------------------------- And corresponding Java code: ----------------------------------------------------------- import java.io.BufferedWriter; import java.io.FileWriter; import java.io.Writer; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.velocity.VelocityContext; import org.apache.velocity.app.Velocity; public class WhitespaceTest { public static void main(String[] args) throws Exception { new WhitespaceTest().run(); } public void run() throws Exception { Velocity.init(); VelocityContext ctx = new VelocityContext(); List menu = new ArrayList(); Map menuElem = new HashMap(); menuElem.put("link", "menu0link"); menuElem.put("imgsrc", "/button_factory?textColor=FFFFFF&BGCOLOR=990000&text=Search"); menu.add(menuElem); ctx.put("menu", menu); Writer writer = new BufferedWriter(new FileWriter("result.htm")); try { boolean result = Velocity.mergeTemplate("whitespace_sample1.vm", "UTF-8", ctx, writer); } finally { writer.close(); } } } ----------------------------------------------------------- result.htm: ----------------------------------------------------------- <TABLE CELLSPACING="0" CELLPADDING="0" WIDTH="600" BORDER="0"> <TR> <TD><A HREF="menu0link"><IMG SRC="/button_factory?textColor=FFFFFF&BGCOLOR=990000&text=Search"></A></TD> <TD><!-- empty button --></TD> <TD><!-- empty button --></TD> <TD><!-- empty button --></TD> <TD><!-- empty button --></TD> </TR> </TABLE> ----------------------------------------------------------- -- )Hiroaki Nakamura) hnakamur@xxxxxxxxxxxxxxx ----- Original Message ----- From: "Rafael Steil" <rafael@xxxxxxxxxxxxxx> To: "Velocity Users List" <velocity-user@xxxxxxxxxxxxxxxxxx> Sent: Wednesday, November 27, 2002 2:56 AM Subject: Re: Formatting Issue (Whitespace) > > This problem ( we can call it "problem", right? :) is really sucks. Some days > ago someone posted an anwser to the same question ( well, not exactly *this* > topic > of course, but the problem was the same ) and him told that there is a > development > patch, or something like that, to fix thi issue. > Will this be fixed in new versions of Velocity? or this is not a bug > but instead a intended feature? > > Rafael (snip) |
|
| <Prev in Thread] | Current Thread | [Next in Thread> |
|---|---|---|
| Previous by Date: | Re: Default to quiet references: 00195, Philip Jenvey |
|---|---|
| Next by Date: | Setting Variables: 00195, Peter Harrison |
| Previous by Thread: | Re: Formatting Issue (Whitespace)i: 00195, Rafael Steil |
| Next by Thread: | Re: Formatting Issue (Whitespace): 00195, Christoph . Reck |
| Indexes: | [Date] [Thread] [Top] [All Lists] |
| News | FAQ | advertise |