You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by "O. Olson" <ol...@yahoo.it> on 2013/08/05 18:25:13 UTC

What does "Velocimacro with a Body" mean?

Hi,
 
            I am new to
Velocity and I am wondering what "Velocimacro with a Body" means?
Here, I am referring to the description in 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
 
            I thought
all Velocimacros had bodies, just like all non-trivial functions in C or Java
have bodies. I attempted the use the above suggestion given in the URL and it did not
work.
 
I attempted the following in my Global Library: 
 
#macro(query_url $query_param) 
    q=$query_param 
#end 
 
#macro(trim) 
$!bodyContent.trim() 
#end  
 
According to the above URL, in my template, I called this
using: 
 
#@trim()#query_url("sometext")#end 
 
 
The result seems to be: 
            q=sometext 
 
i.e. there are spaces in the front that I don't like.
(Depending on the format you are looking at, these preceeding spaces might be
deleted, but they appear in the rendered results.) Any ideas what I am doing
wrong?
 
Thank you in advance, 
O. O.

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


Re: What does "Velocimacro with a Body" mean?

Posted by Sergiu Dumitriu <se...@gmail.com>.
It should work, so debug it by testing everything:

------%<------

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

Just #qu: #query_url("sometext")

Trimmed: #@my_trim()#query_url("sometext")#end

------>%------

On 08/07/2013 05:09 PM, O. Olson wrote:
> 
> 
> Thank you very much Nathan for clarifying what body content
> meant. At least now I know what it means, but I think I am having a problem
> with my trim function here. It seems to delete everything. I have no clue how
> this worked previously.
>  
> To avoid naming conflicts, I decided to rename my macro. It
> now looks like: 
>  #macro(query_url $query_param)
>     q=$query_param
> #end
> 
> #macro(my_trim)$!bodyContent.trim()#end 
> 
>  
>  
> I call it using 
>  #@my_trim()#query_url("sometext")#end 
> 
>  
> And the result is blank i.e. I do not see anything in the
> result. I now attempt: 
> #macro(my_trim)PREPEND$!bodyContent.trim()#end 
> 
>  
> This time I see only:
>  PREPEND 
> 
> This is expected, but why nothing from  $!bodyContent.trim()
> 
> I don't know what is happening to trim() function i.e. why
> it is not working. But thank you for answering my question.
>  
> O. O.
> 
> 
> ----- Messaggio originale -----
> Da: Nathan Bubna <nb...@gmail.com>
> A: Velocity Users List <us...@velocity.apache.org>; O. Olson <ol...@yahoo.it>
> Cc: 
> Inviato: Lunedì 5 Agosto 2013 22:34
> Oggetto: Re: What does "Velocimacro with a Body" mean?
> 
> Yes, all velocimacros have bodies in their definition:
> 
>   #macro( foo ) definition only #end
>   #macro( bar ) definition accepts $bodyContent #end
> 
> Not all velocimacros accept bodies in their usage:
> 
>   #foo()
>   #@bar() body content when used #end
> 
> Produces:
> 
>   definition only
>   definition accepts body content when used
> 
> As to why your example ends up with preceding spaces, i am not
> certain. The "format" part in particular, because Velocity has only
> input text and output text, no "format".  Try defining your #@trim
> like so:
> 
>   #macro( trim )$!bodyContent.trim()#end
> 
> to ensure there are no sneaky whitespaces hiding in the definition.
> 
> 
> On Mon, Aug 5, 2013 at 9:25 AM, O. Olson <ol...@yahoo.it> wrote:
>> Hi,
>>
>>              I am new to
>> Velocity and I am wondering what "Velocimacro with a Body" means?
>> Here, I am referring to the description in 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
>>
>>              I thought
>> all Velocimacros had bodies, just like all non-trivial functions in C or Java
>> have bodies. I attempted the use the above suggestion given in the URL and it did not
>> work.
>>
>> I attempted the following in my Global Library:
>>
>> #macro(query_url $query_param)
>>      q=$query_param
>> #end
>>
>> #macro(trim)
>> $!bodyContent.trim()
>> #end
>>
>> According to the above URL, in my template, I called this
>> using:
>>
>> #@trim()#query_url("sometext")#end
>>
>>
>> The result seems to be:
>>              q=sometext
>>
>> i.e. there are spaces in the front that I don't like.
>> (Depending on the format you are looking at, these preceeding spaces might be
>> deleted, but they appear in the rendered results.) Any ideas what I am doing
>> wrong?
>>
>> Thank you in advance,
>> O. O.

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

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


Re: What does "Velocimacro with a Body" mean?

Posted by Nathan Bubna <nb...@gmail.com>.
D'oh. I should have seen that.  Thanks, Alex!

On Wed, Aug 7, 2013 at 2:25 PM, Alex Fedotov <al...@kayak.com> wrote:
> It does not work because bodyContent is an instance of the ASTNode class and
> not a string, so it does not have the trim method.
>
> Use something like this:
>
> #macro(my_trim)#set($str="$bodyContent")$str.trim()#end
>
> Test:
>
> before#@my_trim()    -blah-     #{end}after
>
> Renders:
> before-blah-after
>
>
> On Wed, Aug 7, 2013 at 5:09 PM, O. Olson <ol...@yahoo.it> wrote:
>>
>>
>>
>> Thank you very much Nathan for clarifying what body content
>> meant. At least now I know what it means, but I think I am having a
>> problem
>> with my trim function here. It seems to delete everything. I have no clue
>> how
>> this worked previously.
>>
>> To avoid naming conflicts, I decided to rename my macro. It
>> now looks like:
>>  #macro(query_url $query_param)
>>     q=$query_param
>> #end
>>
>> #macro(my_trim)$!bodyContent.trim()#end
>>
>>
>>
>> I call it using
>>  #@my_trim()#query_url("sometext")#end
>>
>>
>> And the result is blank i.e. I do not see anything in the
>> result. I now attempt:
>> #macro(my_trim)PREPEND$!bodyContent.trim()#end
>>
>>
>> This time I see only:
>>  PREPEND
>>
>> This is expected, but why nothing from  $!bodyContent.trim()
>>
>> I don't know what is happening to trim() function i.e. why
>> it is not working. But thank you for answering my question.
>>
>> O. O.
>>
>>
>> ----- Messaggio originale -----
>> Da: Nathan Bubna <nb...@gmail.com>
>> A: Velocity Users List <us...@velocity.apache.org>; O. Olson
>> <ol...@yahoo.it>
>> Cc:
>> Inviato: Lunedì 5 Agosto 2013 22:34
>> Oggetto: Re: What does "Velocimacro with a Body" mean?
>>
>> Yes, all velocimacros have bodies in their definition:
>>
>>   #macro( foo ) definition only #end
>>   #macro( bar ) definition accepts $bodyContent #end
>>
>> Not all velocimacros accept bodies in their usage:
>>
>>   #foo()
>>   #@bar() body content when used #end
>>
>> Produces:
>>
>>   definition only
>>   definition accepts body content when used
>>
>> As to why your example ends up with preceding spaces, i am not
>> certain. The "format" part in particular, because Velocity has only
>> input text and output text, no "format".  Try defining your #@trim
>> like so:
>>
>>   #macro( trim )$!bodyContent.trim()#end
>>
>> to ensure there are no sneaky whitespaces hiding in the definition.
>>
>>
>> On Mon, Aug 5, 2013 at 9:25 AM, O. Olson <ol...@yahoo.it> wrote:
>> > Hi,
>> >
>> >             I am new to
>> > Velocity and I am wondering what "Velocimacro with a Body" means?
>> > Here, I am referring to the description in
>> > 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
>> >
>> >             I thought
>> > all Velocimacros had bodies, just like all non-trivial functions in C or
>> > Java
>> > have bodies. I attempted the use the above suggestion given in the URL
>> > and it did not
>> > work.
>> >
>> > I attempted the following in my Global Library:
>> >
>> > #macro(query_url $query_param)
>> >     q=$query_param
>> > #end
>> >
>> > #macro(trim)
>> > $!bodyContent.trim()
>> > #end
>> >
>> > According to the above URL, in my template, I called this
>> > using:
>> >
>> > #@trim()#query_url("sometext")#end
>> >
>> >
>> > The result seems to be:
>> >             q=sometext
>> >
>> > i.e. there are spaces in the front that I don't like.
>> > (Depending on the format you are looking at, these preceeding spaces
>> > might be
>> > deleted, but they appear in the rendered results.) Any ideas what I am
>> > doing
>> > wrong?
>> >
>> > Thank you in advance,
>> > O. O.
>> >
>> > ---------------------------------------------------------------------
>> > To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
>> > For additional commands, e-mail: user-help@velocity.apache.org
>> >
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
>> For additional commands, e-mail: user-help@velocity.apache.org
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
>> For additional commands, e-mail: user-help@velocity.apache.org
>>
>

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


Re: What does "Velocimacro with a Body" mean?

Posted by "O. Olson" <ol...@yahoo.it>.

Thank you very much Erik. (Great to see you here and on the
Solr group.) This is a good tip, because  from it I realized that bodyContent is class
org.apache.velocity.runtime.directive.Block. 
 
So another way to trim from what Alex suggested is:
#macro(my_trim)$!bodyContent.toString().trim()#end
 
Simply doing:
#macro(my_trim)$!bodyContent.trim()#end
 
Does not work.
 
Thanks again,
O. O.


----- Messaggio originale -----
Da: Erik Hatcher <er...@gmail.com>
A: Velocity Users List <us...@velocity.apache.org>; O. Olson <ol...@yahoo.it>
Cc: Sergiu Dumitriu <se...@gmail.com>; Alex Fedotov <al...@kayak.com>
Inviato: Giovedì 8 Agosto 2013 5:36
Oggetto: Re: What does "Velocimacro with a Body" mean?

One trick I use when confronted with context objects that aren't behaving as I'd expect is to output:

    $object.class

and then it's clear what object type you're dealing with.

    Erik

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


Re: What does "Velocimacro with a Body" mean?

Posted by Erik Hatcher <er...@gmail.com>.
One trick I use when confronted with context objects that aren't behaving as I'd expect is to output:

    $object.class

and then it's clear what object type you're dealing with.

	Erik

On Aug 7, 2013, at 18:10 , O. Olson wrote:

> 
> 
> Thank you very much Sergiu  and Alex. 
>  
>             I was just
> about to post to Sergiu that I could not get his suggestion to work, when I saw the post from Alex. 
>  
> Thank you Alex. You are totally
> correct here – your suggestion worked perfectly and solved my problem.
>  
> Thanks again to both of you,
> O. O.
>  
>  
>  
> 
> 
> ----- Messaggio originale -----
> Da: Alex Fedotov <al...@kayak.com>
> A: Velocity Users List <us...@velocity.apache.org>; O. Olson <ol...@yahoo.it>
> Cc: Nathan Bubna <nb...@gmail.com>
> Inviato: Mercoledì 7 Agosto 2013 16:25
> Oggetto: Re: What does "Velocimacro with a Body" mean?
> 
> It does not work because bodyContent is an instance of the ASTNode class
> and not a string, so it does not have the trim method.
> 
> Use something like this:
> 
> #macro(my_trim)#set($str="$bodyContent")$str.trim()#end
> 
> Test:
> 
> before#@my_trim()    -blah-     #{end}after
> 
> Renders:
> before-blah-after
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
> 


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


Re: What does "Velocimacro with a Body" mean?

Posted by "O. Olson" <ol...@yahoo.it>.

Thank you very much Sergiu  and Alex. 
 
            I was just
about to post to Sergiu that I could not get his suggestion to work, when I saw the post from Alex. 
 
Thank you Alex. You are totally
correct here – your suggestion worked perfectly and solved my problem.
 
Thanks again to both of you,
O. O.
 
 
 


----- Messaggio originale -----
Da: Alex Fedotov <al...@kayak.com>
A: Velocity Users List <us...@velocity.apache.org>; O. Olson <ol...@yahoo.it>
Cc: Nathan Bubna <nb...@gmail.com>
Inviato: Mercoledì 7 Agosto 2013 16:25
Oggetto: Re: What does "Velocimacro with a Body" mean?

It does not work because bodyContent is an instance of the ASTNode class
and not a string, so it does not have the trim method.

Use something like this:

#macro(my_trim)#set($str="$bodyContent")$str.trim()#end

Test:

before#@my_trim()    -blah-     #{end}after

Renders:
before-blah-after

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


Re: What does "Velocimacro with a Body" mean?

Posted by Alex Fedotov <al...@kayak.com>.
It does not work because bodyContent is an instance of the ASTNode class
and not a string, so it does not have the trim method.

Use something like this:

#macro(my_trim)#set($str="$bodyContent")$str.trim()#end

Test:

before#@my_trim()    -blah-     #{end}after

Renders:
before-blah-after


On Wed, Aug 7, 2013 at 5:09 PM, O. Olson <ol...@yahoo.it> wrote:

>
>
> Thank you very much Nathan for clarifying what body content
> meant. At least now I know what it means, but I think I am having a problem
> with my trim function here. It seems to delete everything. I have no clue
> how
> this worked previously.
>
> To avoid naming conflicts, I decided to rename my macro. It
> now looks like:
>  #macro(query_url $query_param)
>     q=$query_param
> #end
>
> #macro(my_trim)$!bodyContent.trim()#end
>
>
>
> I call it using
>  #@my_trim()#query_url("sometext")#end
>
>
> And the result is blank i.e. I do not see anything in the
> result. I now attempt:
> #macro(my_trim)PREPEND$!bodyContent.trim()#end
>
>
> This time I see only:
>  PREPEND
>
> This is expected, but why nothing from  $!bodyContent.trim()
>
> I don't know what is happening to trim() function i.e. why
> it is not working. But thank you for answering my question.
>
> O. O.
>
>
> ----- Messaggio originale -----
> Da: Nathan Bubna <nb...@gmail.com>
> A: Velocity Users List <us...@velocity.apache.org>; O. Olson <
> olson_ord@yahoo.it>
> Cc:
> Inviato: Lunedì 5 Agosto 2013 22:34
> Oggetto: Re: What does "Velocimacro with a Body" mean?
>
> Yes, all velocimacros have bodies in their definition:
>
>   #macro( foo ) definition only #end
>   #macro( bar ) definition accepts $bodyContent #end
>
> Not all velocimacros accept bodies in their usage:
>
>   #foo()
>   #@bar() body content when used #end
>
> Produces:
>
>   definition only
>   definition accepts body content when used
>
> As to why your example ends up with preceding spaces, i am not
> certain. The "format" part in particular, because Velocity has only
> input text and output text, no "format".  Try defining your #@trim
> like so:
>
>   #macro( trim )$!bodyContent.trim()#end
>
> to ensure there are no sneaky whitespaces hiding in the definition.
>
>
> On Mon, Aug 5, 2013 at 9:25 AM, O. Olson <ol...@yahoo.it> wrote:
> > Hi,
> >
> >             I am new to
> > Velocity and I am wondering what "Velocimacro with a Body" means?
> > Here, I am referring to the description in
> 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
> >
> >             I thought
> > all Velocimacros had bodies, just like all non-trivial functions in C or
> Java
> > have bodies. I attempted the use the above suggestion given in the URL
> and it did not
> > work.
> >
> > I attempted the following in my Global Library:
> >
> > #macro(query_url $query_param)
> >     q=$query_param
> > #end
> >
> > #macro(trim)
> > $!bodyContent.trim()
> > #end
> >
> > According to the above URL, in my template, I called this
> > using:
> >
> > #@trim()#query_url("sometext")#end
> >
> >
> > The result seems to be:
> >             q=sometext
> >
> > i.e. there are spaces in the front that I don't like.
> > (Depending on the format you are looking at, these preceeding spaces
> might be
> > deleted, but they appear in the rendered results.) Any ideas what I am
> doing
> > wrong?
> >
> > Thank you in advance,
> > O. O.
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> > For additional commands, e-mail: user-help@velocity.apache.org
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
>
>

Re: What does "Velocimacro with a Body" mean?

Posted by "O. Olson" <ol...@yahoo.it>.

Thank you very much Nathan for clarifying what body content
meant. At least now I know what it means, but I think I am having a problem
with my trim function here. It seems to delete everything. I have no clue how
this worked previously.
 
To avoid naming conflicts, I decided to rename my macro. It
now looks like: 
 #macro(query_url $query_param)
    q=$query_param
#end

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

 
 
I call it using 
 #@my_trim()#query_url("sometext")#end 

 
And the result is blank i.e. I do not see anything in the
result. I now attempt: 
#macro(my_trim)PREPEND$!bodyContent.trim()#end 

 
This time I see only:
 PREPEND 

This is expected, but why nothing from  $!bodyContent.trim()

I don't know what is happening to trim() function i.e. why
it is not working. But thank you for answering my question.
 
O. O.


----- Messaggio originale -----
Da: Nathan Bubna <nb...@gmail.com>
A: Velocity Users List <us...@velocity.apache.org>; O. Olson <ol...@yahoo.it>
Cc: 
Inviato: Lunedì 5 Agosto 2013 22:34
Oggetto: Re: What does "Velocimacro with a Body" mean?

Yes, all velocimacros have bodies in their definition:

  #macro( foo ) definition only #end
  #macro( bar ) definition accepts $bodyContent #end

Not all velocimacros accept bodies in their usage:

  #foo()
  #@bar() body content when used #end

Produces:

  definition only
  definition accepts body content when used

As to why your example ends up with preceding spaces, i am not
certain. The "format" part in particular, because Velocity has only
input text and output text, no "format".  Try defining your #@trim
like so:

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

to ensure there are no sneaky whitespaces hiding in the definition.


On Mon, Aug 5, 2013 at 9:25 AM, O. Olson <ol...@yahoo.it> wrote:
> Hi,
>
>             I am new to
> Velocity and I am wondering what "Velocimacro with a Body" means?
> Here, I am referring to the description in 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
>
>             I thought
> all Velocimacros had bodies, just like all non-trivial functions in C or Java
> have bodies. I attempted the use the above suggestion given in the URL and it did not
> work.
>
> I attempted the following in my Global Library:
>
> #macro(query_url $query_param)
>     q=$query_param
> #end
>
> #macro(trim)
> $!bodyContent.trim()
> #end
>
> According to the above URL, in my template, I called this
> using:
>
> #@trim()#query_url("sometext")#end
>
>
> The result seems to be:
>             q=sometext
>
> i.e. there are spaces in the front that I don't like.
> (Depending on the format you are looking at, these preceeding spaces might be
> deleted, but they appear in the rendered results.) Any ideas what I am doing
> wrong?
>
> Thank you in advance,
> O. O.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
>

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

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


Re: What does "Velocimacro with a Body" mean?

Posted by Nathan Bubna <nb...@gmail.com>.
Yes, all velocimacros have bodies in their definition:

  #macro( foo ) definition only #end
  #macro( bar ) definition accepts $bodyContent #end

Not all velocimacros accept bodies in their usage:

  #foo()
  #@bar() body content when used #end

Produces:

  definition only
  definition accepts body content when used

As to why your example ends up with preceding spaces, i am not
certain. The "format" part in particular, because Velocity has only
input text and output text, no "format".  Try defining your #@trim
like so:

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

to ensure there are no sneaky whitespaces hiding in the definition.


On Mon, Aug 5, 2013 at 9:25 AM, O. Olson <ol...@yahoo.it> wrote:
> Hi,
>
>             I am new to
> Velocity and I am wondering what "Velocimacro with a Body" means?
> Here, I am referring to the description in 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
>
>             I thought
> all Velocimacros had bodies, just like all non-trivial functions in C or Java
> have bodies. I attempted the use the above suggestion given in the URL and it did not
> work.
>
> I attempted the following in my Global Library:
>
> #macro(query_url $query_param)
>     q=$query_param
> #end
>
> #macro(trim)
> $!bodyContent.trim()
> #end
>
> According to the above URL, in my template, I called this
> using:
>
> #@trim()#query_url("sometext")#end
>
>
> The result seems to be:
>             q=sometext
>
> i.e. there are spaces in the front that I don't like.
> (Depending on the format you are looking at, these preceeding spaces might be
> deleted, but they appear in the rendered results.) Any ideas what I am doing
> wrong?
>
> Thank you in advance,
> O. O.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
>

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