[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [cgiapp] Global vars management in templates


On 9/13/05, Wojciech Pietron <suppressed> wrote:
> There are some variables that I would like to have defined in all templates
> I am going to use, ie. name of the user, url, sysdate etc.
> 
> The question is: what is the simplest/best practice to maintain global template
> variables and not redefine them all after loading a new template? I am
> specially interested in CAP::AnyTemplate environment.

I know you are looking for AnyTemplate answers, but I thought I would
mention how this is done with the TT plugin.  Everytime you call
tt_process to render a template, an automatic call is made to
tt_pre_process which will receive the template name, and all template
parameters that were passed to tt_process.  This allows you to alter
the parameters at the last moment before the template is rendered. 
here is the relevant section of the docs:

       tt_pre_process

       This is an overridable method that works in the spirit of
       cgiapp_prerun.  The method will be called just before a
       template is processed, and will be passed the same parame-
       ters that were passed to tt_process (ie the template file-
       name, and a hashref of template parameters).  It can be
       used to make last minute changes to the template, or the
       parameters before the template is processed.

Here is a quick example that adds the session object to every template:

sub tt_pre_process {
  my $self = shift;
  my $template = shift;
  my $vars = shift;

  $vars->{session} = $self->session;
}

I believe there is similar functionality in AnyTemplate, but I don't
know the exact syntax...  Check the docs

Cheers,

Cees

---------------------------------------------------------------------
Web Archive:  http://www.mail-archive.com/suppressed/
              http://marc.theaimsgroup.com/?l=cgiapp&r=1&w=2
To unsubscribe, e-mail: suppressed
For additional commands, e-mail: suppressed


Mail converted by mhonarc 2.6.15
This archive provided courtesy of JSW4.NET, Internet Hosting Services for Small Business.