NAME

Template::Object - lightweight object-oriented template engine


SYNOPSIS

example.tpl

    <h1>{{title}}</h1> 
    <!-- BEGIN list --> 
    <ul> 
    <!-- BEGIN item --> 
        <li>{{name}} are {{attr}}</li> 
    <!-- END item --> 
    </ul> 
    <!-- END list --> 
    <!-- BEGIN empty --> 
    <p>The list is empty.</p> 
    <!-- END empty -->

example.pl

    use Template::Object;
    my @colors = (
        { name => 'apples', attr => 'red' },
        { name => 'bananas', attr => 'yellow' },
        { name => 'people', attr => 'blue' },
    );
    Template::Object->root('templates');
    my $t = Template::Object->new(file => 'example.tpl');
    $t->vars(title => 'Colors');
    # TMTOWTDI!
    # my $t = Template::Object->new(file => 'example.tpl')->vars(title => 'Colors');
    # my $t = Template::Object->new(file => 'example.tpl')->vars(title => [ 'Colors' ]);
    # my $t = Template::Object->new(file => 'example.tpl')->vars({ title => 'Colors' });
    # my $t = Template::Object->new(file => 'example.tpl')->vars({ title => [ 'Colors' ] });
    # my $t = Template::Object->new(file => 'example.tpl', vars => { title => 'Colors' });
    # my $t = Template::Object->new(file => 'example.tpl', vars => { title => [ 'Colors' ] });
    # my $t = Template::Object->new({ file => 'example.tpl', vars => { title => [ 'Colors' ] } });
    if (@colors) {
        my ($list) = map { $t->extract_block($_) } qw/list empty/;
        foreach (@colors) {
            my $item = $list->extract_block('item');
            
            # TMTOWTDI!
            #my $item = $list->extract_block('item', { class => 'Template::Object' });
            
            $item->vars($_); # TMTOWTDI! ...
            $list->vars(item => $item, { append => 1 });
            
            # TMTOTDI!
            # $list->vars({ item => $item }, { append => 1 });
            # $list->vars({ item => [ $item ] }, { append => 1 });
        }
        $t->vars(list => $list);
    }
    else {
        my ($empty) = map { $t->extract_block($_) } qw/empty list/;
        $t->vars(empty => $empty);
    }
    print $t;


COPYRIGHT AND LICENSE

Copyright (C) 2006-2009, Ivan Fomichev.

This program is free software, you can redistribute it and/or modify it under the terms of the Artistic License version 2.0.