I have released a new version of Template::Parser::CET.  This new
version of Parser::CET is based off of the CGI::Ex::Template module
which was recently moved to the Template::Alloy namespace.

One of the more prominent features of the new version of
Template::Parser::CET is that it allows you to use HTML::Template,
HTML::Template::Expr, Text::Tmpl, and Velocity (VTL) style templates
with your current installation of Template::Toolkit.  And of course
Parser::CET still supports Template::Toolkit v2 and v3.  Parser::CET
will parse the templates to Template::Alloy's AST - which will then be
translated to a Template::Document string by Parser::CET.  The
Template::Document can then be cached and used by the rest of the TT
engine as if they were native TT templates.  And because it is the Alloy
engine which represents the mixing of the template engines, you can
use features from each of the languages in templates written in any
of the languages (except for HTML::Template mode because we wanted
to stay true to "HTML::Template simplicity rules").


I have a number of proposals that I'll submit emails that will follow
this one.  But in the interim - you can try Template::Parser::CET with
your TT installation now.  I have included some samples of template
usage for those who may be curious - but not curious enough to install
Parser::CET.

Enjoy.

Paul Seamons

------------------- HTML::Template example --------------------------
#!/usr/bin/perl

use strict;

use Template;
use Template::Parser::CET;
Template::Parser::CET->activate;

my $str = <<'EOD';
    foo = (<TMPL_VAR NAME=foo>)
    ---------
    bar
    <TMPL_LOOP NAME=bar>
      <TMPL_VAR __counter__>. name = <TMPL_VAR name>
    </TMPL_LOOP>
EOD

my $swap = {
    foo => 'Foo',
    bar => [{name => 'bingo'},
            {name => 'bango'},
            {name => 'boingo'},
            ],
};

# allow for private variables (the HT loop context variables)
local $Template::Stash::PRIVATE = 0;

# set the syntax and ask for context var creation
my $t = Template->new(SYNTAX => 'ht', LOOP_CONTEXT_VARS => 1);

$t->process(\$str, $swap);

# uncomment to see how the document was parsed
#print Template::Parser::CET->new(SYNTAX => 'ht')->dump_parse_tree(\$str);

__END__
    # prints out the following

    foo = (Foo)
    ---------
    bar

      1. name = bingo

      2. name = bango

      3. name = boingo


------------------- HTML::Template::Expr example --------------------------
#!/usr/bin/perl

use strict;

use Template;
use Template::Parser::CET;
Template::Parser::CET->activate;

my $str = <<'EOD';
    <TMPL_VAR EXPR="1 + (2 * 3)">
    <TMPL_LOOP NAME=bar>
      name = <TMPL_VAR EXPR=sprintf("(%s)", name)>
    </TMPL_LOOP>
EOD

my $swap = {
    bar => [{name => 'bingo', },
            {name => 'bango', },
            {name => 'boingo',},
            ],
};

# add all Text VMethods to the stash
Template::Parser::CET->add_top_level_functions($swap);

my $t = Template->new(SYNTAX => 'hte');
$t->process(\$str, $swap);


# uncomment to see how the document was parsed
#print Template::Parser::CET->new(SYNTAX => 'hte')->dump_parse_tree(\$str);

__END__
    # prints out the following

      name = (bingo)

      name = (bango)

      name = (boingo)



------------------- HTML::Template::Expr example with TT 
extensions -------------
#!/usr/bin/perl

use strict;

use Template;
use Template::Parser::CET;
Template::Parser::CET->activate;

## HTE builtins (such as IF) require EXPR
## TT extensions such as GET don't support EXPR
## notice the use of chomping

my $str = <<'EOD';
    <TMPL_GET 1 + 2 * 3 ->
    <TMPL_FOREACH b IN bar>
      <-TMPL_IF EXPR="loop.count == 1">
        One
      <-TMPL_ELSIF EXPR="loop.count == 2">
        Two $b.name
      <-TMPL_ELSE>
        <TMPL_GET b.name>
      </-TMPL_IF>
    </-TMPL_LOOP>
EOD

my $swap = {
    bar => [{name => 'bingo' },
            {name => 'bango' },
            {name => 'boingo'},
            ],
};

my $t = Template->new(SYNTAX => 'hte', INTERPOLATE => 1);
$t->process(\$str, $swap);

# uncomment to see how the document was parsed
#print Template::Parser::CET->new(SYNTAX => 'hte')->dump_parse_tree(\$str);

__END__
    # prints out the following

    7
        One
        Two bango
        boingo


------------------- Text::Tmpl example ------------------
#!/usr/bin/perl

use strict;

use Template;
use Template::Parser::CET;
Template::Parser::CET->activate;

my $str = <<'EOD';
    <!-- echo $foo -->
    <!-- loop "bar" -->
    name = <!-- echo $name -->
    <!-- endloop -->
    <!-- comment --> This is not shown <!-- endcomment -->
EOD

my $swap = {
    foo => 'Foo',
    bar => [{name => 'bingo' },
            {name => 'bango' },
            {name => 'boingo'},
            ],
};

my $t = Template->new(SYNTAX => 'tmpl', INTERPOLATE => 1);
$t->process(\$str, $swap);

# uncomment to see how the document was parsed
#print Template::Parser::CET->new(SYNTAX => 'tmpl')->dump_parse_tree(\$str);

__END__
    # prints out the following

    Foo

    name = bingo

    name = bango

    name = boingo

------------------- Velocity (VTL) example ------------------
#!/usr/bin/perl

use strict;

use Template;
use Template::Parser::CET;
Template::Parser::CET->activate;

my $str = <<'EOD';
    $foo
    #foreach(b in bar)
    name = $b.name
    #end
    #* This is not shown *#
EOD

my $swap = {
    foo => 'Foo',
    bar => [{name => 'bingo' },
            {name => 'bango' },
            {name => 'boingo'},
            ],
};

my $t = Template->new(SYNTAX => 'velocity', INTERPOLATE => 1);
$t->process(\$str, $swap);

# uncomment to see how the document was parsed
#print Template::Parser::CET->new(SYNTAX 
=> 'velocity')->dump_parse_tree(\$str);

__END__
    # prints out the following

    Foo

    name = bingo

    name = bango

    name = boingo

_______________________________________________
templates mailing list
templates@template-toolkit.org
http://lists.template-toolkit.org/mailman/listinfo/templates

Reply via email to