On 08/01/2013 02:02 PM, O. Olson wrote:
> 
> 
> Thank you very much Alex. This would be very painful if
> every time I call a macro, I would need to set a variable and trim it. 
>  
>             I am
> curious what is the function of the @ sign in the helper “trim” macro that you
> suggested. By the way, how do I call this macro i.e. invoke it?

This is a macro with a body:
http://velocity.apache.org/engine/releases/velocity-1.7/vtl-reference-guide.html#amacro_-_Allows_users_to_define_a_Velocimacro_VM_a_repeated_segment_of_a_VTL_template_as_required

The full code would be:

#macro(trim)
$!bodyContent.trim()
#end

#macro(default_query_url)
... your normal macro body, with all the extra space ...
#end

Calling it:

#@trim()#default_query_url()#end

>             Is it possible to return a particular variable from a Web Macro? 
> E.g.
>  
> #macro(default_query_url)
>     #if($request.params.get('q'))
>           #set($str =
> query_url($request.params.get('q')))
>     #else
>           #set($str =
> query_url("*"))
>             #end
>             RETURN $str
> ???? How???

No.

Macros are not functions.

But any variable you set will still be available after the macro call
ends, so you can use it afterwards. You could do something like:

#set ($discard = "#default_query_url()")
$str

This will just discard anything the macro would normally print, and you
just print the variable that was set inside the macro.

Note that this behavior is optional, and the velocity configuration can
disable it.

> #end
>  
> This would help my whitespace problem.
>  
> Thanks again,
> O. O.
> 
> 
> ----- Messaggio originale -----
> Da: Alex Fedotov <a...@kayak.com>
> A: Velocity Users List <user@velocity.apache.org>; O. Olson 
> <olson_...@yahoo.it>
> Cc: 
> Inviato: Giovedì 1 Agosto 2013 12:43
> Oggetto: Re: Returning String without Whitespace from Velocimacros
> 
> Simple way:
> 
> #set($str="#default_query_url()")
> $str.trim()
> 
> Can probably create a helper macro "trim" that does the same thing to the
> body content:
> 
> #@trim()
>    #default_query_url()
> #end
> 
> 
> 
> 
> On Thu, Aug 1, 2013 at 1:27 PM, O. Olson <olson_...@yahoo.it> 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