[Petal] Nice way of doing includes

Fergal Daly fergal at esatclear.ie
Wed Jul 9 13:42:38 BST 2003


While writing the last email, I thought of a very nice feature of being able 
to use expressions in the method arguments. You can now use included 
templates that take arguments. So you can have includes that are like 
subroutines.

At the moment, an included template uses the same hash as the current one so 
you have to worry about variable names clashing between the included template 
and any file that includes it but now you can write templates like 
subroutines and not worry about what's happening outside them.

Create a package like this.

package Processor;

sub new
{
  return bless {}, shift;
}

sub go
{
	my $pkg = shift;
	my $template = shift;
	my $p = Petal->new($template);
	return $p->process(@_);
}

then in your script

my %hash;
$hash{P} = Processor->new
# fill up the rest of your hash

and you can do

<DIV petal::content="structure P/go 'show_user.xml ' 'user ' current_user" />

which will load up show_user.xml and run it with

->process(user => $hash->{current_user});

and you can even make the name of the template dynamic and do something like

<DIV petal:replace="structure P/go(Headers/get_template(user/header_prefs), 
'user', user)" />

so depending on what user/header_prefs is set to, it goes and gets the header 
template and then passes in the user,

F



More information about the Petal mailing list