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

cvs@template-toolkit.org cvs@template-toolkit.org
Wed, 10 Nov 2004 18:12:06 +0000


cvs         04/11/10 18:12:06

  Modified:    lib/Template Tagset.pm
  Log:
  * updates to init() method
  * added switch() method
  
  Revision  Changes    Path
  1.2       +82 -42    TT3/lib/Template/Tagset.pm
  
  Index: Tagset.pm
  ===================================================================
  RCS file: /template-toolkit/TT3/lib/Template/Tagset.pm,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- Tagset.pm	2004/11/08 18:46:59	1.1
  +++ Tagset.pm	2004/11/10 18:12:06	1.2
  @@ -16,7 +16,7 @@
   #   modify it under the same terms as Perl itself.
   #
   # REVISION
  -#   $Id: Tagset.pm,v 1.1 2004/11/08 18:46:59 abw Exp $
  +#   $Id: Tagset.pm,v 1.2 2004/11/10 18:12:06 abw Exp $
   #
   #========================================================================
   
  @@ -24,14 +24,14 @@
   
   use strict;
   use warnings;
  +use Template::Utils;
   use Template::Base;
  -use vars qw( $VERSION $DEBUG $ERROR $TAGS );
   use base qw( Template::Base );
   
  -$VERSION = sprintf("%d.%02d", q$Revision: 1.1 $ =~ /(\d+)\.(\d+)/);
  -$DEBUG   = 0 unless defined $DEBUG;
  -$ERROR   = '';
  -$TAGS    = { };
  +our $VERSION = sprintf("%d.%02d", q$Revision: 1.2 $ =~ /(\d+)\.(\d+)/);
  +our $DEBUG   = 0 unless defined $DEBUG;
  +our $TAGS    = { } unless defined $TAGS;
  +our $ERROR   = '';
   
   
   # alias tag() to tags()
  @@ -47,20 +47,31 @@
   
   sub init {
       my ($self, $config) = @_;
  -    my $pkgtags = $self->pkgvar( TAGS => $TAGS );
  -    my $cfgtags = $config->{ tags };
   
       $self->debug("init() { ", join(', ', %$config), " }\n")
           if $DEBUG;
   
  -    # if a 'tags' items isn't specified explicitly in the config hash
  -    # then we assume the entire config hash is the tags definition
  -    unless ($cfgtags) {
  -        $self->debug("no 'tags' item specified, using entire hash\n") if $DEBUG;
  -        $cfgtags = $config;
  -        $config  = { };
  -    }
  +    # Fetch 'tags' from config options, deleting it on the way.  We
  +    # have to remove any arguments destined for us (tags, order,
  +    # disabled and enabled) from the config hash which we later pass
  +    # onto as initialisation parameters to any tags or nested tagsets
  +    # we create
  +
  +    my $cfgtags = delete($config->{ tags }) || { };
  +
  +    # order can be a list ref or string of non-word character separated tag names
  +    my $order = delete($config->{ order }) || [ ];
  +    $order = [ split(/\W+/, $order) ] 
  +        unless UNIVERSAL::isa($order, 'ARRAY');
   
  +    # enabled or disabled flags can be set, default to enabled
  +    my $enabled = defined $config->{ disabled }
  +                ? delete( $config->{ disabled } ) ? 0 : 1 
  +                : defined $config->{ enabled  }
  +                ? delete( $config->{ enabled  } ) ? 1 : 0
  +                : 1;
  +
  +
       # In some cases it is necessary to specify an order for the tags.
       # This is used when constructing a regex to match the tags' start
       # tokens to ensure that one tag appears in the regex before
  @@ -71,13 +82,9 @@
       # order is insignificant.  A separate 'order' config item can be
       # provided to indicate a desired order for some or all of the tags.
   
  -    my $tags  = { };
  -    my $order = $config->{ order } || [ ];
  +    my $pkgtags = $self->pkgtags($config) || return;
  +    my $tags    = { };
       
  -    # order can be a string of non-word character separated tag names
  -    $order = [ split(/\W+/, $order) ] 
  -        unless UNIVERSAL::isa($order, 'ARRAY');
  -
       # process package tags first, config tags second
       foreach my $tagset ($pkgtags, $cfgtags) {
           if (UNIVERSAL::isa($tagset, 'ARRAY')) {
  @@ -117,23 +124,22 @@
   
       $self->debug('order: ', join(', ', @$order), "\n") if $DEBUG;
   
  -    # set tags and order internally
  -    $self->{ tags  } = $tags;
  -    $self->{ order } = $order;
  -
  -    # look for the 'disabled' or 'enabled' flags in the config
  -    # or assume the tag is enabled by default
  -    if (defined $config->{ disabled }) {
  -        $self->{ enabled } = $config->{ disabled } ? 0 : 1;
  -    }
  -    elsif (defined $config->{ enabled }) {
  -        $self->{ enabled } = $config->{ enabled } ? 1 : 0;
  -    }
  -    else {
  -        $self->{ enabled } = 1;
  -    }
  +    # Now initialise any tag items, turning any values that are strings
  +    # (indicating class names, e.g. Template::Tag::Variable) into objects, 
  +    # by calling the new() method.  We pass the config hash (or what's 
  +    # left of it after extracting our items) to propagate any values
  +    # passed by the caller.  
  +
  +    @$tags{ keys %$tags } = map { 
  +        ref($_) ? $_ : $_->new(%$config) 
  +            || return $self->error("failed to create $_: ", $_->error());
  +    } values %$tags;
  +
  +    # set tags, order and enabled internally
  +    $self->{ tags    } = $tags;
  +    $self->{ order   } = $order;
  +    $self->{ enabled } = $enabled;
   
  -    # call the order() method to compute the 
       return $self;
   }
   
  @@ -235,9 +241,17 @@
           my @regex = map { 
               UNIVERSAL::isa($_, 'Regexp') ? $_ : quotemeta($_);
             } @{ $tagmap->{ every_start } };
  -        my $regex = join( '|', @regex);
  -        $tagmap->{ regex } = qr/ \G (.*?) ($regex) /sx;
  -        $self->debug("tagmap regex: $regex\n") if $DEBUG;
  +
  +        if (@regex) {
  +            my $regex = join( '|', @regex);
  +            $tagmap->{ regex } = qr/ \G (.*?) ($regex) /sx;
  +            $self->debug("tagmap regex: $regex\n") if $DEBUG;
  +        }
  +        else {
  +            # regex that matches nothing 
  +            $tagmap->{ regex } = qr//;
  +            $self->debug("no tagmap regex\n") if $DEBUG;
  +        }
       }
   
       return $tagmap;
  @@ -299,6 +313,32 @@
   
   
   #------------------------------------------------------------------------
  +# switch( $type, $flag )
  +#
  +# Method to call the enable() or disable() method on the tag/tagset named
  +# by $type, according to the value passed in $flag.  Any false or undefined
  +# value, or the string 'off' (case insensitive) will disable the tag, any 
  +# other true value will enable it.  If no flag is passed then a reference
  +# to the tag or tagset object is returned.
  +#------------------------------------------------------------------------
  +
  +sub switch {
  +    my $self = shift;
  +    my $type = shift;
  +    my $tag  = $self->tags($type) || return;
  +    
  +    if (@_) {
  +        my $flag = shift;
  +        $flag = 0 if ($flag =~ /^off$/i);
  +        return $flag ? $tag->enable() : $tag->disable();
  +    }
  +    else {
  +        return $tag;
  +    }
  +}
  +
  +
  +#------------------------------------------------------------------------
   # enable()
   # disable()
   #
  @@ -346,14 +386,14 @@
   
   
   #------------------------------------------------------------------------
  -# pkgtags()
  +# pkgtags($config)
   #
   # Return the $TAGS hash reference in the subclass package if defined, or
   # in the base class package if not.
   #------------------------------------------------------------------------
   
   sub pkgtags {
  -    my $self  = shift;
  +    my $self = shift;
       return $self->pkgvar( TAGS => $TAGS );
   }
   
  @@ -419,7 +459,7 @@
   
   =head1 VERSION
   
  -$Revision: 1.1 $
  +$Revision: 1.2 $
   
   =head1 COPYRIGHT