[Petal] Dynamic Templates with Petal

Jean-Michel Hiver jhiver at mkdoc.com
Sun Jul 4 00:19:27 BST 2004


Warren Smith wrote:
> I'm having troubles getting dynamic includes to work. I'm trying to do
> something like this:
> 
> --- site.html
> <html>
>   ... header stuff ...
>     <div metal:use-macro="${content_template}#content"></div>
>   ... footer stuff ...
> </html>

Err... ouch - that's not going to work. TAL processing happens once the 
template has been compiled as a subroutine. METAL stuff is done at 
compile-time, and at that stage there is no context to take the value from.


I think the way you could do this would be defining a 'dyna_include' 
modifier, i.e. something like:


package Petal::Hash::Dyna_Include;
use base Petal::Hash::String;
use warnings;
use strict;

sub process
{
     my $self = shift;
     my $hash = shift;
     my $args = shift;

     # turns ${content_template}#content into what it should be
     my $template_name = $self->SUPER::process ($hash, $args);

     # process the template and returns the result
     my $template = new Petal ($template_name);
     return $template->process ($hash);
}


1;


__END__


Make sure your Dyna_Include file is somewhere in your @INC path, i.e. if 
you're having:

use lib ('.');

Then it should be in ./Petal/Hash/Dyna_Include.pm


Once you've done that, your template becomes:

<!-- structure keyword is here to avoid double-encoding -->
<div
   petal:content="structure dyna_include: ${content_template}#content">

Gimmick Mockup

</div>


This solution is untested, but it should work. Let me know how it goes...


Cheers,
Jean-Michel.


More information about the Petal mailing list