On Fri, Mar 28, 2003 at 11:17:21AM -0500, Brett Sanger wrote:
> What I'd like to do is get wonderful levels of template reuse.  Because
> we'll have different instances of the same modules, I'd like to be able
> to point the module to a directory for templates, and have every
> PROCESS-like calls that start at the directory, and 'fall' upward to the
> template root (we don't allow absolute paths).   

Could you not just set the INCLUDE_PATH as appropriate?

    my $template = Template->new( INCLUDE_PATH => [qw(
        /foo/bar/baz
        /foo/bar
        /foo
    )]);

You can define a subroutine or object as an element in the INCLUDE_PATH
if you want to generate a path list dynamically.

    my $template = Template->new( INCLUDE_PATH => [
        '/foo/bar/baz',
        sub { return [EMAIL PROTECTED] },
        '/ping/pong'
    ]);

> So I'm looking at the best way to approach this.  I'd like to not use a
> Plugin -- the USE calls will just add complexity to an approach that I'm
> trying to make more simple.  I've looked into defining a subroutine
> that's passed to the template call, but a  PROCESS Cascade('some_path')
> call treated Cascade as a filename rather than a subroutine call.  

That should be [% PROCESS $Cascade('some_path') %] to tell TT that 
Cascade is a variable.

> If there's a way to
> avoid that, that seems the best option, so I can just call
> Cascade('some path')  in place of PROCESS.

You could write a plugin something like this:

   package Template::Plugin::Cascade;
   use Template::Plugin;
   use base qw( Template::Plugin );

   sub new {
       my ($class, $context, $path) = @_;
       bless {
           context => $context,
           path    => $path,
       }, $class;
   }

   sub include {
       my ($self, $file, $args) = @_;
       $self->{ context }->include($file, $args);
   }

Then you would use it something like this:

   [% USE Cascade %]
   [% Cascade.include('foo', pi=3.14 e=2.718) %]

By directing the call at the Template::Context, rather that the parent 
Template object, you bypass any header/footer malarky and just get the
one template processed.

> Can I do what I seek, or am I headed in a wrong path?  Which of the many
> M's should I check to RTFM?

I'd suggest you look at implementing it by a dynamic INCLUDE_PATH element,
or by subclassing Template::Provider with a custom paths() subroutine to
determine the list of search paths.

See my earlier post for how you could use a PREFIX_MAP to bind particular
INCLUDE/PROCESS templates to the custom cascade provider.

   [% INCLUDE cascade:header %]

That's probably the nicest general solution.

HTH
A


_______________________________________________
templates mailing list
[EMAIL PROTECTED]
http://lists.template-toolkit.org/mailman/listinfo/templates

Reply via email to