You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Praveen Ray <pr...@yahoo.com> on 2005/10/19 04:31:13 UTC

calling same macro in multiple pages

Hi All
I'm trying to abstract the common layout in one .vm file
like this:

layout.vm:
-----------
<html>
<body>
<div> #parse( "header.vm" ) </div>
<div> #body() </div>
<div> #parse( "footer.vm") </div>
</body>
</html>

homepage.vm
-----------
#macro( body )
This is Home Page
goto <a href='/myinfo'>My Info</a>
#end
#parse( "layout.vm")

myinfo.vm
---------
#macro( body )
This is My Info
#end
#parse( "layout.vm")


As you can see the general layout of the site has been
abstracted into layout.vm and each individual template is 
just generating the content for the main body. Although
nice scheme, this doesn't work. The homepage displays
properly, however clicking on 'My Info' link on the home,
takes you a screen which has same content as the body of
the homepage. It's, as if, the #body macro from homepage is
used to render myinfo page. Looks like similar named macros
from different pages are not unique?

Is this kind of stuff even doable in Velocity? If yes, what
am I doing wrong? If not, can someone please suggest an
alternate way of abstracting the layout in one file?
Thanks


  - Praveen  


	
		
__________________________________ 
Yahoo! Mail - PC Magazine Editors' Choice 2005 
http://mail.yahoo.com

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


Re: calling same macro in multiple pages

Posted by Simon Christian <si...@stoutstick.com>.
Hi Praveen,

By default, you can't redefine a macro once it has been set. Thus  
when you attempt to set the #body macro for the second time, it's  
ignored (there may well be a message in the logs). You can change  
this behavior using the config parameter:

     velocimacro.permissions.allow.inline.local.scope = true

This will mean the macro doesn't 'exist' outside the defining template.

However, there is a better way...the kind of layout you're trying to  
achieve is handled more neatly (and with less work for you) by the  
VelocityLayoutServlet, part of the Velocity Tools subproject which  
allow two-pass rendering of the template. I strongly suggest you take  
a look:

     http://jakarta.apache.org/velocity/tools/view/layoutservlet.html

- simon


On 19 Oct 2005, at 03:31, Praveen Ray wrote:

> Hi All
> I'm trying to abstract the common layout in one .vm file
> like this:
>
> layout.vm:
> -----------
> <html>
> <body>
> <div> #parse( "header.vm" ) </div>
> <div> #body() </div>
> <div> #parse( "footer.vm") </div>
> </body>
> </html>
>
> homepage.vm
> -----------
> #macro( body )
> This is Home Page
> goto <a href='/myinfo'>My Info</a>
> #end
> #parse( "layout.vm")
>
> myinfo.vm
> ---------
> #macro( body )
> This is My Info
> #end
> #parse( "layout.vm")
>
>
> As you can see the general layout of the site has been
> abstracted into layout.vm and each individual template is
> just generating the content for the main body. Although
> nice scheme, this doesn't work. The homepage displays
> properly, however clicking on 'My Info' link on the home,
> takes you a screen which has same content as the body of
> the homepage. It's, as if, the #body macro from homepage is
> used to render myinfo page. Looks like similar named macros
> from different pages are not unique?
>
> Is this kind of stuff even doable in Velocity? If yes, what
> am I doing wrong? If not, can someone please suggest an
> alternate way of abstracting the layout in one file?
> Thanks
>
>
>   - Praveen
>
>
>
>
> __________________________________
> Yahoo! Mail - PC Magazine Editors' Choice 2005
> http://mail.yahoo.com
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>
>


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