[Catalyst-dev] Re: Tutorial updates...

Ash Berlin ash_cpan at firemirror.com
Sun May 25 11:35:44 BST 2008


On 25 May 2008, at 00:46, Aristotle Pagaltzis wrote:

> * Ash Berlin <ash_cpan at firemirror.com> [2008-05-25 01:20]:
>> On 24 May 2008, at 20:35, Marcus Ramberg wrote:
>>> I personally dislike specially the fact that the TTSite helper
>>> changes the context object name to Catalyst, and that it
>>> splits up the template into path into src and lib which I find
>>> unnecessary and confusing.
>>
>> src and lib would make sense if the providers were setup right
>> such that the top level template only gets searched for in src,
>> and PROCESS, INCLUDE etc only looked for under lib. But since
>> this isn't the case, agreed, there is little point.
>
> Do you know of a way to do that? I haven’t seen any way of asking
> TT to use a particular search path for PROCESS/INCLUDE only, and
> I do find it annoying to have all my includes and wrappers mixed
> with all the page templates in a single namespace.
>
> Regards,
> --  

Don't expect this to work directly with a catalyst app - was taken  
from an old framework I used to work on,. But all the logic/important  
bits are there

use base 'Template';

sub new {
   my $class = shift;
   my $app_class = shift;

   my $confing = $app_class->config_hash;
   my $cache   = $config->{template_cache} || catdir($config- 
 >{templates}, "..", "cache");
   my $libdirs = [ $templates ];

   # we're creating two providers, but they can both share the same
   # parser for efficiency
   my $parser = Template::Config->parser({
   }) || return $class->error(Template::Config->error());

   # create a web provider which knows where to find the web page
   # templates (separate from the library of template components)
   my $webprov = Template::Config->provider({
     PARSER       => $parser,
     ABSOLUTE     => 1,
   }) || return $class->error(Template::Config->error());


   # now create a regular TT engine
   my $self = $class->SUPER::new({
     INCLUDE_PATH => $libdirs,
     PARSER       => $parser,
   }) || return $class->error(Template::Config->error());

   # stuff some extra USERDATA into the object for later
   $self->{ USERDATA } = {
     WEB_PROVIDER => $webprov,
   };

   $self;
}

sub process {
   my ($self, $input, $vars) = @_;

   # this is the provider that looks in the templates and shared  
templates
   # directory. It knows how to look up the templates that represent  
pages on
   # the webserver.  Our superclass doesn't know how to look for these  
directly.
   # It only knows how to look for 'library' templates.  This ensures  
that the
   # library 'pages' can only be accessed indirectly, not directly
   my $webprov = $self->{ USERDATA }->{ WEB_PROVIDER };

   # fetch and compile source template from web provider
   my ($template, $error) = $webprov->fetch($input);

   if ($error) {
     # declined (i.e. not found?) or another kind of loading error
     if ($error == &Template::Constants::STATUS_DECLINED) {
       $logger->error('DECLINED', ': ', "file '$input' not found");
     }
     else {
       $logger->error("error: $template)");
     }

     # any which way, that didn't work
     $logger->logdie("failed to process page '".$input."': ".$self- 
 >error."'");
   }

   # process using self with regular provider (which may load additional
   # library templates itself as needed.)
   my $output;
   $self->SUPER::process($template, $vars, \$output)
     or $logger->logdie("failed to process page '".$input."': '".$self- 
 >error."'");

   return $output;
}






More information about the Catalyst-dev mailing list