[Templates-cvs] cvs commit: TT3/lib/Template Component.pm

cvs@template-toolkit.org cvs@template-toolkit.org
Fri, 26 Mar 2004 10:55:11 +0000


cvs         04/03/26 10:55:11

  Modified:    lib/Template Component.pm
  Log:
  * changed search() method to allow providers to implement either a
    fetch($context, $item, @args) method (for those that wants the current
    context reference) or get($item, @args) for those that don't.
  
  Revision  Changes    Path
  1.3       +23 -12    TT3/lib/Template/Component.pm
  
  Index: Component.pm
  ===================================================================
  RCS file: /template-toolkit/TT3/lib/Template/Component.pm,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- Component.pm	2004/03/25 16:20:10	1.2
  +++ Component.pm	2004/03/26 10:55:11	1.3
  @@ -32,7 +32,7 @@
   #     else
   #
   # REVISION
  -#   $Id: Component.pm,v 1.2 2004/03/25 16:20:10 abw Exp $
  +#   $Id: Component.pm,v 1.3 2004/03/26 10:55:11 abw Exp $
   #
   #========================================================================
   
  @@ -46,7 +46,14 @@
   use vars qw( $VERSION $DEBUG $ERROR $MAX_DEPTH );
   use base qw( Template::Base );
   
  -$VERSION   = sprintf("%d.%02d", q$Revision: 1.2 $ =~ /(\d+)\.(\d+)/);
  +# providers can define a fetch($context, $item) method which expects a
  +# component reference passed as the first argument, or a get($item)
  +# method which doesn't receive a context reference
  +
  +use constant FETCH => 'fetch';
  +use constant GET   => 'get';
  +
  +$VERSION   = sprintf("%d.%02d", q$Revision: 1.3 $ =~ /(\d+)\.(\d+)/);
   $DEBUG     = 0 unless defined $DEBUG;
   $ERROR     = '';
   $MAX_DEPTH = 64;
  @@ -543,7 +550,7 @@
       my $locations = $self->{ locations }->{ $resource }
           || $self->locate($resource) || return;
   
  -    my ($items, $fetch);
  +    my ($items, $method);
   
       eval {
           # provide a useful exception type for errors thrown
  @@ -553,14 +560,19 @@
               if (ref($items) eq 'HASH') {
                   # look for the item in the hash
                   last if defined ($item = $items->{ $name });
  +            }
  +            elsif ($method = UNIVERSAL::can($items, FETCH)) {
  +                # call fetch() method on provider, passing $self
  +                last if defined ($item = &$method($items, $self, $name, @opts));
               }
  -            elsif ($fetch = UNIVERSAL::can($items, 'fetch')) {
  -                # call on a provider to fetch it
  -                last if ($item = &$fetch($items, $name, @opts));
  +            elsif ($method = UNIVERSAL::can($items, GET)) {
  +                # call get() method, without passing $self
  +                last if defined ($item = &$method($items, $name, @opts));
               }
               elsif (UNIVERSAL::isa($items, 'CODE')) {
  -                # call subroutine to fetch it
  -                last if ($item = &$items($self, $name, @opts));
  +                # call subroutine to fetch it, passing $self as first
  +                # argument so that subroutine can behave like a method
  +                last if defined ($item = &$items($self, $name, @opts));
               }
               else {
                   $self->warning("invalid $resource collection: $items");
  @@ -629,9 +641,7 @@
               if $self->{ DEBUG };
       }
       else {
  -        $self->debug("looking for templates resource\n") if $self->{ DEBUG };
           if ($templates = $self->resource('templates')) {
  -            $self->debug("using templates resource: $templates\n") if $self->{ DEBUG };
               if ($template = $templates->fetch($self, $name, $opts)) {
                   $cache->{ $name } = $template;
                   $self->debug("stored $name in used templates cache\n")
  @@ -686,7 +696,8 @@
           }
           elsif ($name eq 'default') {
               $name = $compiler;
  -            $self->debug("looking for default compiler: $name\n") if $self->{ DEBUG };
  +            $self->debug("looking for default compiler: $name\n") 
  +                if $self->{ DEBUG };
               $compiler = $self->search( compilers => $name );
               return unless defined $compiler;
               return $self->decline("$name default compiler not found") 
  @@ -820,7 +831,7 @@
   
   =head1 VERSION
   
  -$Revision: 1.2 $
  +$Revision: 1.3 $
   
   =head1 COPYRIGHT