Velocimacros are macros, they don't "return" anything. They print stuff,
just like normal code outside the macro. A macro is a simple way of
reusing code without duplicating it.

As in other Velocity code, almost all whitespace is important, so you
have to be careful if you don't want it to be printed.

Still, it is possible to control all the whitespace produced, reducing
the "style" of the code, combining:

- ## at the end of the line to comment out the \n that would otherwise
be printed
- The formal syntax for #{else} ${var} #{end} to eliminate the need for
a separator space after the end of these words
- Removing leading whitespace, thus breaking the indentation

Another option is to use a pre-processor that trims the whole .vm file
before parsing it, so instead of sending the original template to the
Velocity engine, you send a string with all the leading and trailing
space removed.

Another option is to run the macro inside a string, which you can then trim:

#set ($tmp = "#query_url($query_param)")
${tmp.trim()}

On 08/01/2013 01:27 PM, O. Olson wrote:
> Hi,
> 
> I am new to using Apache Velocity. What is the correct way of returning a 
> string from a Macro or a Velocimacro? 
> 
> Since I did not have a clue on how to return a string from a Macro, I decided 
> to do something like 
> #macro(query_url $query_param) q=$query_param #end 
> 
> I can then call the Macro using: #query_url("*")
> 
> The problem with this is that the resulting value contains a Tab or 
> number of Spaces before the 'q=' when I call it. 
> 
> 
> One option is to delete the spaces like: 
> #macro(query_url $query_param)
>        q=$query_param
> #end 
> This fixes my problem but is very hard to read.  To take this a step further 
> consider: 
> #macro(default_query_url) #if($request.params.get('q')) 
> #query_url($request.params.get('q')) #else #query_url("*") #end #end 
> Vs. 
> #macro(default_query_url)
>     #if($request.params.get('q'))
>           #query_url($request.params.get('q'))
>     #else
>           #query_url("*")
> #end 
> In the first case there is no whitespace, but editing it would be very 
> difficult for more complicated macros. 
> 
> I am curious what is the correct way to return values from Velocimacros and 
> if I can Trim the resulting Whitespace before returning it?
> 
> Thank you an advance for any help.
> O. O.


-- 
Sergiu Dumitriu
http://purl.org/net/sergiu

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@velocity.apache.org
For additional commands, e-mail: user-h...@velocity.apache.org

Reply via email to