You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Mark de Reeper <ma...@yahoo.com> on 2001/09/05 03:43:51 UTC

Dynamic macro names?

Is it possible to call a macro where part/all of the name is dynamic?

e.g.

    #set( $themename = "default" )
    #${themename}header()

I do not expect it to work based on what I read in the documentation.
Maybe there is a better way to achieve what this is trying to do...

I want to define a bunch of web themes, i.e. table headers/footers etc
that can be referenced as macros. This allows me to have them all
loaded at startup of Velocity and available to all templates. The
macro's therefor need unique names.

I want to be able to switch to different themes, based on it's name.

The nice thing about having the theme elements as macro's is that I can
keep them in a template file rather than stored away in some properties
file, which makes it easier for a HTML type person to create them.

Having them as macro's also means that each theme macro can refer to
existing context elements.

Ideas?


Cheers

Mark

__________________________________________________
Do You Yahoo!?
Get email alerts & NEW webcam video instant messaging with Yahoo! Messenger
http://im.yahoo.com

Re: Dynamic macro names?

Posted by Mark de Reeper <ma...@yahoo.com>.
Patrick,

That is my current workaround, a set of support macros that know about
all of the themes and calls the appropriate theme based on the name.

It works but is a bit of an overhead as I currently have about 10 theme
macros per theme which makes it 10 if/elseif thingys to support fore
each call.

e.g.

#macro( themeheader )
    #if( $config.currentTheme == "mdr" )
        #mdrthemeheader()
    #elseif( $config.currentTheme == "default" )
        #defaultthemeheader()
    #end
#end



Thanks

Mark

--- "Patrick E. Whitesell" <pw...@clickability.com> wrote:
> Mark de Reeper wrote:
> 
> >Is it possible to call a macro where part/all of the name is
> dynamic?
> >
> >e.g.
> >
> >    #set( $themename = "default" )
> >    #${themename}header()
> >
> 
> Why not define your macro as:
> 
> #macro(header $themename)
> #if($themename = "default")
> shmoo
> #elseif($themename = "theme1")
> shmoo
> ...
> #else
> blah
> #end
> #end
> 
> 
> and call it like:
> 
> #header($themename)
> 
> >
> >I do not expect it to work based on what I read in the
> documentation.
> >Maybe there is a better way to achieve what this is trying to do...
> >
> >I want to define a bunch of web themes, i.e. table headers/footers
> etc
> >that can be referenced as macros. This allows me to have them all
> >loaded at startup of Velocity and available to all templates. The
> >macro's therefor need unique names.
> >
> >I want to be able to switch to different themes, based on it's name.
> >
> >The nice thing about having the theme elements as macro's is that I
> can
> >keep them in a template file rather than stored away in some
> properties
> >file, which makes it easier for a HTML type person to create them.
> >
> >Having them as macro's also means that each theme macro can refer to
> >existing context elements.
> >
> >Ideas?
> >
> >
> >Cheers
> >
> >Mark
> >
> >__________________________________________________
> >Do You Yahoo!?
> >Get email alerts & NEW webcam video instant messaging with Yahoo!
> Messenger
> >http://im.yahoo.com
> >
> 
> -- 
> Patrick E. Whitesell
> pwhitesell@clickability.com
> 
> ***************************************
> This e-mail message may contain confidential and/or legally
> privileged information. If you are not the intended recipient(s), or
> the employee or agent responsible for delivery of this message to the
> intended recipient(s), you are hereby notified that any
> dissemination, distribution or copying of this e-mail message is
> strictly prohibited.If you have received this message in error,
> please immediately notify the sender and delete this e-mail message
> from your computer. DO NOT FORWARD THIS E-MAIL MESSAGE WITHOUT THE
> PERMISSION OF THE SENDER.
> 
> 
> 


__________________________________________________
Do You Yahoo!?
Get email alerts & NEW webcam video instant messaging with Yahoo! Messenger
http://im.yahoo.com

Re: Dynamic macro names?

Posted by "Patrick E. Whitesell" <pw...@clickability.com>.
Mark de Reeper wrote:

>Is it possible to call a macro where part/all of the name is dynamic?
>
>e.g.
>
>    #set( $themename = "default" )
>    #${themename}header()
>

Why not define your macro as:

#macro(header $themename)
#if($themename = "default")
shmoo
#elseif($themename = "theme1")
shmoo
...
#else
blah
#end
#end


and call it like:

#header($themename)

>
>I do not expect it to work based on what I read in the documentation.
>Maybe there is a better way to achieve what this is trying to do...
>
>I want to define a bunch of web themes, i.e. table headers/footers etc
>that can be referenced as macros. This allows me to have them all
>loaded at startup of Velocity and available to all templates. The
>macro's therefor need unique names.
>
>I want to be able to switch to different themes, based on it's name.
>
>The nice thing about having the theme elements as macro's is that I can
>keep them in a template file rather than stored away in some properties
>file, which makes it easier for a HTML type person to create them.
>
>Having them as macro's also means that each theme macro can refer to
>existing context elements.
>
>Ideas?
>
>
>Cheers
>
>Mark
>
>__________________________________________________
>Do You Yahoo!?
>Get email alerts & NEW webcam video instant messaging with Yahoo! Messenger
>http://im.yahoo.com
>

-- 
Patrick E. Whitesell
pwhitesell@clickability.com

***************************************
This e-mail message may contain confidential and/or legally privileged information. If you are not the intended recipient(s), or the employee or agent responsible for delivery of this message to the intended recipient(s), you are hereby notified that any dissemination, distribution or copying of this e-mail message is strictly prohibited.If you have received this message in error, please immediately notify the sender and delete this e-mail message from your computer. DO NOT FORWARD THIS E-MAIL MESSAGE WITHOUT THE PERMISSION OF THE SENDER.




Re: Dynamic macro names?

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 9/4/01 9:43 PM, "Mark de Reeper" <ma...@yahoo.com> wrote:

> 
> Is it possible to call a macro where part/all of the name is dynamic?
> 
> e.g.
> 
>   #set( $themename = "default" )
>   #${themename}header()

No

> 
> I do not expect it to work based on what I read in the documentation.
> Maybe there is a better way to achieve what this is trying to do...

There has to be, because the above isn't going to work :)
 
> I want to define a bunch of web themes, i.e. table headers/footers etc
> that can be referenced as macros. This allows me to have them all
> loaded at startup of Velocity and available to all templates. The
> macro's therefor need unique names.
> 
> I want to be able to switch to different themes, based on it's name.
> 
> The nice thing about having the theme elements as macro's is that I can
> keep them in a template file rather than stored away in some properties
> file, which makes it easier for a HTML type person to create them.
> 
> Having them as macro's also means that each theme macro can refer to
> existing context elements.
> 
> Ideas?

There are two ways that jump to mind at 5:30 am  (it's 5:30 here...)

1) Use Velocity to generate a VM library in a separate step that you then
load in the webapp.

2) Since 1) is no fun - you could use the Velocity / VelocityeEngine API to
iteratively create your macros dynamically, and then take the output, a
string, and run that through evaluate() to register the VMs.  I won't go
into long detail (it's 5:30 am...) but if this interests you, I (or others)
can fill in the details.

Geir



Re: Dynamic macro names?

Posted by Jon Stevens <jo...@latchkey.com>.
on 9/4/01 6:43 PM, "Mark de Reeper" <ma...@yahoo.com> wrote:

> Is it possible to call a macro where part/all of the name is dynamic?

I doubt it. Did you try it?

> e.g.
> 
>   #set( $themename = "default" )
>   #${themename}header()
> 
> I do not expect it to work based on what I read in the documentation.
> Maybe there is a better way to achieve what this is trying to do...
> 
> I want to define a bunch of web themes, i.e. table headers/footers etc
> that can be referenced as macros. This allows me to have them all
> loaded at startup of Velocity and available to all templates. The
> macro's therefor need unique names.
> 
> I want to be able to switch to different themes, based on it's name.
> 
> The nice thing about having the theme elements as macro's is that I can
> keep them in a template file rather than stored away in some properties
> file, which makes it easier for a HTML type person to create them.
> 
> Having them as macro's also means that each theme macro can refer to
> existing context elements.

You really should consider using a framework like Turbine which allows you
to define Layouts and Navigations (ie: headers/footers/pieces of foo). Doing
this with macro's is not a good idea.

-jon