You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by kris <kr...@gmail.com> on 2013/04/12 07:46:55 UTC

macro fails to access param properties with render.eval

My VTL template looks like:
#macro(aMacro $par1)
	##$par1
	$par1.getId()
#end

#set($macroName = "aMacro") 
#set( $call = "#$macroName('$p1')" )
#evaluate($call)

I have filled the context with an object that has Id property. When I run
this template I would like to see the Id of the object. Instead it just
outputs "$par1.getId()" 



--
View this message in context: http://velocity.10973.n7.nabble.com/macro-fails-to-access-param-properties-with-render-eval-tp16194.html
Sent from the Velocity - User mailing list archive at Nabble.com.

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


Re: macro fails to access param properties with render.eval

Posted by kris <kr...@gmail.com>.
I got this working!!!

#set($macroName = "aMacro")
#set( $call = '#'+$macroName+'($p1)' )
#evaluate($call)


On Sat, Apr 13, 2013 at 7:34 AM, Krishna Sarma <kr...@gmail.com>wrote:

> Hello Jonathan,
> thanks for your reply.
>
> It works great when I call #aMacro($p1). But in my case, the macro to
> call would be determined dynamically. I would construct the macro name
> something like :
>     #$objectx.type+$objecty.operation($objectz)
>
> In my example, if I remove the quotes around $p1, system throws an error
> like "unrecognized string..." Also, I believe with those quotes, VE is not
> considering $p1 as plain string literal. When I uncomment the first line in
> aMacro, it is printing the object reference, not the string "$p1".
>
> Is there any workaround to invoke the macro dynamically with parameters?
>
> Thanks
>
>
> On Fri, Apr 12, 2013 at 7:11 PM, Jonathan Ihm [via Velocity] <
> ml-node+s10973n16195h94@n7.nabble.com> wrote:
>
>> You are calling a method on a string.  When you put $p1 between the
>> single
>> quotes, you are not using whatever object $p1 is, but the literal string
>> '$p1' and getId() is not a method for a string.
>>
>> Also, it works fine for me when I run #aMacro($p1) with a dummy object,
>> but
>> not setting macroName and call variables.  Are those necessary?  Try
>> this:
>>
>> #aMacro($p1)
>>
>>
>>
>>
>> On Fri, Apr 12, 2013 at 12:46 AM, kris <[hidden email]<http://user/SendEmail.jtp?type=node&node=16195&i=0>>
>> wrote:
>>
>> > My VTL template looks like:
>> > #macro(aMacro $par1)
>> >         ##$par1
>> >         $par1.getId()
>> > #end
>> >
>> > #set($macroName = "aMacro")
>> > #set( $call = "#$macroName('$p1')" )
>> > #evaluate($call)
>> >
>> > I have filled the context with an object that has Id property. When I
>> run
>> > this template I would like to see the Id of the object. Instead it just
>> > outputs "$par1.getId()"
>> >
>> >
>> >
>> > --
>> > View this message in context:
>> >
>> http://velocity.10973.n7.nabble.com/macro-fails-to-access-param-properties-with-render-eval-tp16194.html
>> > Sent from the Velocity - User mailing list archive at Nabble.com.
>> >
>> > ---------------------------------------------------------------------
>> > To unsubscribe, e-mail: [hidden email]<http://user/SendEmail.jtp?type=node&node=16195&i=1>
>> > For additional commands, e-mail: [hidden email]<http://user/SendEmail.jtp?type=node&node=16195&i=2>
>> >
>> >
>>
>>
>> ------------------------------
>>  If you reply to this email, your message will be added to the
>> discussion below:
>>
>> http://velocity.10973.n7.nabble.com/macro-fails-to-access-param-properties-with-render-eval-tp16194p16195.html
>>  To unsubscribe from macro fails to access param properties with
>> render.eval, click here<http://velocity.10973.n7.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=16194&code=a3Jpc2huYXNhcm1hQGdtYWlsLmNvbXwxNjE5NHwtMTMxNTcwNzg3NQ==>
>> .
>> NAML<http://velocity.10973.n7.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>>
>
>
>
> --
> Thanks
> Krishna Sarma
>



-- 
Thanks
Krishna Sarma




--
View this message in context: http://velocity.10973.n7.nabble.com/macro-fails-to-access-param-properties-with-render-eval-tp16194p16200.html
Sent from the Velocity - User mailing list archive at Nabble.com.

Re: macro fails to access param properties with render.eval

Posted by kris <kr...@gmail.com>.
Hello Jonathan,
thanks for your reply.

It works great when I call #aMacro($p1). But in my case, the macro to call
would be determined dynamically. I would construct the macro name something
like :
    #$objectx.type+$objecty.operation($objectz)

In my example, if I remove the quotes around $p1, system throws an error
like "unrecognized string..." Also, I believe with those quotes, VE is not
considering $p1 as plain string literal. When I uncomment the first line in
aMacro, it is printing the object reference, not the string "$p1".

Is there any workaround to invoke the macro dynamically with parameters?

Thanks


On Fri, Apr 12, 2013 at 7:11 PM, Jonathan Ihm [via Velocity] <
ml-node+s10973n16195h94@n7.nabble.com> wrote:

> You are calling a method on a string.  When you put $p1 between the single
> quotes, you are not using whatever object $p1 is, but the literal string
> '$p1' and getId() is not a method for a string.
>
> Also, it works fine for me when I run #aMacro($p1) with a dummy object,
> but
> not setting macroName and call variables.  Are those necessary?  Try this:
>
> #aMacro($p1)
>
>
>
>
> On Fri, Apr 12, 2013 at 12:46 AM, kris <[hidden email]<http://user/SendEmail.jtp?type=node&node=16195&i=0>>
> wrote:
>
> > My VTL template looks like:
> > #macro(aMacro $par1)
> >         ##$par1
> >         $par1.getId()
> > #end
> >
> > #set($macroName = "aMacro")
> > #set( $call = "#$macroName('$p1')" )
> > #evaluate($call)
> >
> > I have filled the context with an object that has Id property. When I
> run
> > this template I would like to see the Id of the object. Instead it just
> > outputs "$par1.getId()"
> >
> >
> >
> > --
> > View this message in context:
> >
> http://velocity.10973.n7.nabble.com/macro-fails-to-access-param-properties-with-render-eval-tp16194.html
> > Sent from the Velocity - User mailing list archive at Nabble.com.
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: [hidden email]<http://user/SendEmail.jtp?type=node&node=16195&i=1>
> > For additional commands, e-mail: [hidden email]<http://user/SendEmail.jtp?type=node&node=16195&i=2>
> >
> >
>
>
> ------------------------------
>  If you reply to this email, your message will be added to the discussion
> below:
>
> http://velocity.10973.n7.nabble.com/macro-fails-to-access-param-properties-with-render-eval-tp16194p16195.html
>  To unsubscribe from macro fails to access param properties with
> render.eval, click here<http://velocity.10973.n7.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=16194&code=a3Jpc2huYXNhcm1hQGdtYWlsLmNvbXwxNjE5NHwtMTMxNTcwNzg3NQ==>
> .
> NAML<http://velocity.10973.n7.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>



-- 
Thanks
Krishna Sarma




--
View this message in context: http://velocity.10973.n7.nabble.com/macro-fails-to-access-param-properties-with-render-eval-tp16194p16199.html
Sent from the Velocity - User mailing list archive at Nabble.com.

Re: macro fails to access param properties with render.eval

Posted by Jonathan Ihm <jo...@gmail.com>.
You are calling a method on a string.  When you put $p1 between the single
quotes, you are not using whatever object $p1 is, but the literal string
'$p1' and getId() is not a method for a string.

Also, it works fine for me when I run #aMacro($p1) with a dummy object, but
not setting macroName and call variables.  Are those necessary?  Try this:

#aMacro($p1)




On Fri, Apr 12, 2013 at 12:46 AM, kris <kr...@gmail.com> wrote:

> My VTL template looks like:
> #macro(aMacro $par1)
>         ##$par1
>         $par1.getId()
> #end
>
> #set($macroName = "aMacro")
> #set( $call = "#$macroName('$p1')" )
> #evaluate($call)
>
> I have filled the context with an object that has Id property. When I run
> this template I would like to see the Id of the object. Instead it just
> outputs "$par1.getId()"
>
>
>
> --
> View this message in context:
> http://velocity.10973.n7.nabble.com/macro-fails-to-access-param-properties-with-render-eval-tp16194.html
> Sent from the Velocity - User mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscribe@velocity.apache.org
> For additional commands, e-mail: user-help@velocity.apache.org
>
>