You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Peter Sojan <il...@gmx.net> on 2002/04/19 02:58:21 UTC

macros which take bodies ...

Hi all!

I wonder wether this has been discussed before: macros which do not 
only take parameters but also a body which is enclosed in the macro
call. What I mean is the following:

Let's supose for a moment that we have a special $body variable available 
in macros, look at the following simple example: 

#macro( table $padding )
  <table cellspacing="0" cellpadding="$padding" border="0">
     $body
  </table>
#end 

a call might then look like (just a suggestion): 

#table( 2 ) {
  <tr><td> bla </td></tr>
}



Of course this simple example does not show how powerful this might 
be. Its getting interesting when you do nesting:

#macro( row )
  <tr><td> 
     $body
  </td></tr>
#end 

#table( 2 ) {
  #row() { "row one" }
  #row() { "row two" }
  #row() { 
     #table( 0 ) {
  	#row() { "row three" }
  	#row() { "row four" }
     }	
  }
}

You know what I mean ...


How hard would it be to implement such a construct? Would this 
be useful? Has such a thing been considered already? Comments are
welcome ...


Peter


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: macros which take bodies ...

Posted by Peter Sojan <il...@gmx.net>.
On Fri, Apr 19, 2002 at 09:02:15AM -0400, Geir Magnusson Jr. wrote:
> 
> I think I get it. - you want to have a notation where you can specify the
> value of body :
> 
>   #<macroname>( <macro arg>* ) [ { <$body value> } ]
> 
> Right?

exactly! 

> I think that you can fake it a bit already :
> 
> #macro(table $body)
>   <table>
>   $body
>   </table>
> #end
> 
> #macro(row $body)
>   <tr><td>$body</td></tr>
> #end
> 
> #table( "#row('row one') #row('row two')" )
> 
> Which should output
> <table>
> <tr><td>row one</td></tr> <tr><td>row two</td></tr>
> </table>
>
> Is that where you were going with this?

yes ... but this is limited to one-liners. In (my :) ideal world the "body" part 
should take any valid block of VTL code (except a new macro definition statement 
of course). This is what I think would make it hard to implement, because Velocity 
doesn't "work like that" .. I think ...

Peter



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: macros which take bodies ...

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
On 4/18/02 8:58 PM, "Peter Sojan" <il...@gmx.net> wrote:

> 
> Hi all!
> 
> I wonder wether this has been discussed before: macros which do not
> only take parameters but also a body which is enclosed in the macro
> call. What I mean is the following:
> 
> Let's supose for a moment that we have a special $body variable available
> in macros, look at the following simple example:
> 
> #macro( table $padding )
> <table cellspacing="0" cellpadding="$padding" border="0">
>    $body
> </table>
> #end 
> 
> a call might then look like (just a suggestion):
> 
> #table( 2 ) {
> <tr><td> bla </td></tr>
> }
> 
> 
> 
> Of course this simple example does not show how powerful this might
> be. Its getting interesting when you do nesting:
> 
> #macro( row )
> <tr><td> 
>    $body
> </td></tr>
> #end 
> 
> #table( 2 ) {
> #row() { "row one" }
> #row() { "row two" }
> #row() { 
>    #table( 0 ) {
> #row() { "row three" }
> #row() { "row four" }
>    }    
> }
> }
> 
> You know what I mean ...

I think I get it. - you want to have a notation where you can specify the
value of body :

  #<macroname>( <macro arg>* ) [ { <$body value> } ]

Right?
 
> 
> How hard would it be to implement such a construct? Would this
> be useful? Has such a thing been considered already? Comments are
> welcome ...

I think that you can fake it a bit already :

#macro(table $body)
  <table>
  $body
  </table>
#end

#macro(row $body)
  <tr><td>$body</td></tr>
#end

#table( "#row('row one') #row('row two')" )

Which should output
<table>
<tr><td>row one</td></tr> <tr><td>row two</td></tr>
</table>


Not perfect, but a start...

Is that where you were going with this?

-- 
Geir Magnusson Jr.                                     geirm@optonline.net
System and Software Consulting
My inner cowboy needs to yodel.


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>