NAME

Top Close Open

Template::TT3::Type - base class for Text, List and Hash objects

SYNOPSIS

Top Close Open
# defining a Thing subclass object
package Template::TT3::Type::Thing;
use base 'Template::TT3::Type';

our $METHODS = {
    wibble => \&wibble,
    wobble => \&wobble,
};

sub wibble {
    my $self = shift;
    # some wibble code...
}

sub wobble {
    my $self = shift;
    # some wobble code...
}

DESCRIPTION

Top Close Open

The Template::TT3::Type module implements a base class for the Template::TT3::Type::Text , Template::TT3::Type::List and Template::TT3::Type::Hash virtual objects. These implement the virtual methods that can be applied to text, list and hash items using the dot operator:

    [% text = 'Hello World' %]
    [% text.length %]            # 11

    [% list = [10, 20, 30] %]
    [% list.size %]              # 3

    [% hash = { x=10, y=20 } %]
    [% hash.size %]              # 2

They can also be used to create objects for those who prefer to do things in a stricter object-oriented style.

[% text = Text.new('Hello World')  %]
[% list = List.new(10, 20, 30)     %]
[% hash = Hash.new(x = 10, y = 20) %]

TT3 uses Template::TT3::Variable objects to represent variables internally. When a variable is first accessed in a template, the Template::Variables module responsible for managing variables creates a variable object to represent it.

Variables that contain scalar text (or numbers which we treat as just another kind of text for all intents and purposes) are represented using Template::TT3::Variable::Text objects. Hash array references use Template::TT3::Variable::Hash objects, list references use Template::TT3::Variable::List reference, and so on. Undefined values get their own special variable type, Template::TT3::Variable::Undef .

In each case, these variable objects have a corresponding Template::TT3::Type module which defines the virtual methods applicable to that type.

METHODS

Top Close Open

The following methods are defined in addition to those inherited from Template::TT3::Base and Badger::Base .

init(\%config)

Top Close Open

Initialialisation method to handle any per-object initialisation. This is called by the new() method inherited from Badger::Base . In this base class, the method simply copies all items in the $config hash array into the $self object.

This method can also be called directly to add any further items to the object. Named parameters can be provided as a list or by reference to a hash array, as per the new() method.

$object->init( phi => 1.618 );

clone()

Top Close Open

Create a copy of the current object.

my $clone = $object->clone();

Additional named parameters can be provided. These are merged with the items defined in the parent object and passed to the cloned object's init() method.

my $clone = $object->clone( g => 0.577 );

methods()

Top Close Open

Returns a reference to a hash array containing the content of the $METHODS package variable in the current class and any base classes.

my $methods = $object->methods;

method($name)

Top Close Open

Returns a reference to a particular method from the hash reference returned by the methods() method.

my $method = $object->method('ref');

When called without any arguments, it returns a reference to the entire hash reference, as per methods() .

my $method = $object->method->{ foo };

ref()

Top Close Open

Returns the name of the object type, e.g. Template::TT3::Type, Template::TT3::Type::Text, Template::TT3::Type::List , etc., exactly as Perl's ref() function does.

AUTHOR

Top Close Open

Andy Wardley http://wardley.org/

COPYRIGHT

Top Close Open

Copyright (C) 1996-2008 Andy Wardley. All Rights Reserved.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.


http://tt3.template-toolkit.org/docs/Template/TT3/Type.pm last modified 2009-12-22 09:59:12