One of the prime design drive of velocity was to keep it simple.
Your wish seems ok, maybe with some polishing... (see embedded comment ) but would move velocity into a *real* scripting area. Your proposal would be understandable by programmers, but find little understanding by template designers.
The same programmers would also manage to work with arrays and maps (great new feature in the velocity HEAD version!).
So keeping it simple, the new velocity should allow to code your wish using something like:
#macro( mywidget $map ) <h1>Hello $map.foo and $map.bar</h1> #end
From my template I would call:
#set( $bob = "mary") #mywidget( { foo:$bob, bar:"alice" } )
Which would produce the same output you requested!
And the greatest thing is that this already works! - I mean in theory; I havn't tested it yet :D Geir, is the map syntax allowed in a macro call parameter? - otherwise you will need to define a reference first: #set( $params = { foo:$bob, bar:"alice" } ) #mywidget( $params )
Bert, What do you think? Do you agree? Try it out with the nightly build and report back if it works. Thanks.
Cheers, Christoph
Bert van Brakel wrote:
Hi Guys.
I like the idea of macros, however I would like to extend it some more. I would like to be able to set named variables for a macro. I will call them 'widgets'.
A widget definition would be similiar to a macro with some changes:
#widget( mywidget foo=$first bar=$second ) <h1>Hello $first and $second</h1> #end
Some polishing... Why are you proposing to map foo to first? It makes no sense to use two different names to refer to the same value: why does the caller need to use foo= and the widget body $first?
In other threads people where proposing to use this syntax to define defaults:
#widget( mywidget $foo="missing" $bar="none" ) <h1>Hello $foo and $bar</h1> #end
#set( $bob = "mary") #mywidget( $foo=$bob )
Would output <h1>Hello mary and none</h1>
Since $bar was not defined, thus uses the default.
From my template I would call:
#set( $bob = "mary") #widget( mywidget foo=$bob bar="alice" )
Which would produce:
<h1>Hello mary and alice</h1>
A slightly larger example could be:
as a widget call:
#widget( hobbiesTable title="Hobbies Table" onMouseOver="Javascript:jump" linker="$linker" items="${hobbies}" bgcolor="blue" footer="Table 3")
or as a macro call:
#macro( hobbiesTable "Hobbies Table" ${hobbies} "Javascript:jump" $linker "blue" "Table 3")
The widget one is obviously much longer but also a lot clearer. It is easier to see what is set without having to lookup the macro and count the variable position. Order of the variables isn't important, and if more are added they can be put anywhere in the widget definition without affecting templates which already use it.
variables which aren't set are null.
So spec is:
#widget( [widget-name [var-name=[local-var-name]]* ] )
#end
I really don't have a clue how the internals of velocity work so I don't know how hard it is to implement, but it's something that I've been wanting for quite some time and haven't found any other way to do. To my mind it doesn't break velocity prinicipals, and from the templates designers point of view is very easy to use.
Thanks for listening.
-Bert
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
-- :) Christoph Reck
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]