NAME

Template::YetAnother

INTRODUCTION

Why another templating system?

There are a lot of templating modules on CPAN. Some are obvious, some are hidden in very strange namespaces (eg HTML::Processor). Some are used a lot, some not. I read a lot of manpages, but definitly not all and none completly. If there is a module doing what I am proposing, please inform me!

Before we continue, read Perrin Harkins' "Choosing a Templating System", available here

There are different types of Templating Systems available. Some are complete Application Frameworks, including stuff like Session Management, Form Handling etc. Examples include Mason, AxKit and Embperl. They are nice. They work. But that's not what I'm looking for.

I want Just Templates.

Why?

Because IMO, the main reason for using templates is to seperate code from markup. The code produces some data. The markup displays the data. Those Application Frameworks don't seem to be too good at seperating code and markup (I have to admit though, that I know next to nothing about them, only that they are too big/powerfull). After all, they embed code in markup.

So I am looking for a "pipeline"-type, Just-Template System, which reduces the number of available modules somewhat.

The best-known contestors here are TemplateToolkit resp. Apache::Template and HTML::Template. But if you look at there manpages, you'll quickly find references to stuff like "TPL_LOOP" (HTML::Template). TT2 even has it's own mini-language. So, once again, code (even rather trivial) mixed with the markup.

There is one module, CGI::FastTemplate, that does seperate code from markup completly. But the way different templates are strung together seems rather comlicated to me.

But why is there no Templating System with a clean seperation of code and markup?

There are two types of code (at least) that pollute nearly all Templating Systems:

So I am looking for a Templating System that does Just Templating, no code in the markup, maybe by using recursion and OO.

I didn't find anything.

So I am proposing this:

Template::YetAnother

The name is just a placeholder right now, other ideas are: Template::YetAnother is yet another Templating module, using a slightly different approach than most of the other Templating modules.

The templates are completly dumb. There is absolutly no piece of code in a template - neither Perl nor "mini language". A template consists of arbitrary text (e.g. HTML) and Template Tags, e.g. % title %

Your application builds up a data structure. The data structure consists of various Perl Data Types (Strings, Arrays, Hashes) and Template::YetAnother Objects (or Data Structures marked with some other kind of metainformation, e.g. with attributes)

The data structure gets passed to Template::YetAnother, which magically find the right template for each object and replaces all Template Tags (recursivly) with the dumped/stringified data structure.

Template::YetAnother is like Data::Dumper on steroids. It's the big Stringifyer.

Template::YetAnother doesn't use one monolithic template, but a lot of small template fragments, each one correlating to a data type generated by the application.

Template::YetAnother is just an idea right now. I am trying the "write documentation, write tests, write code" way of development... There is only a small prove-of-concept type bit of code (I can send it/post it if somebody cares..). I'll really appreciate feedback on this.

I hope that this descripction is clear enought. If not, let me know and I'll post some clarification / examples.

SYNOPSIS

# generate a new template handler my $th=Template::YetAnother->new ({ namespace=>'acme', template_dir=>'/projects/acme/templates/', }); # build up a data structure $data={ title=>$th->title('this is the title'), breadcrumb=>[ $th->link({url=>'/index.html',text=>'Home'}), $th->separator_breadcrumb, $th->link({url=>'/acme/index.html',text=>'Acme'}), $th->separator_breadcrumb, $th->link({url=>'/acme/bleach.html',text=>'Bleach'}), ], content=>[ $th->element({heading=>'SYNOPSIS',value=>'blabla'}), $th->element({heading=>'DESCRIPTION',value=>'foo bar'}), ], lastmod=>scalar localtime, }; # fill template & print print $th->fill({data=>$data}); ################################################## # for this to work, we need the following files in # /projects/acme/templates # file: main.tpl <html><head><title>[% title %]</title></head> <body> <center>[% breadcrumb %]</center> <h1>[% title %]</h1> [% content %] <hr> [% lastmod %] # file: link.tpl <a href='[% url %]'>[% text %]</a> # file: seperator.tpl / # file: element.tpl <h3>[% heading %]</h3>; <p>[% value %]</p>; ################################################## # the finished template should look like this: <html><head><title>this is the title</title></head> <body> <center> <a href='/index.html'>Home</a> / <a href='/acme/index.html'>Acme</a> / <a href='/acme/bleach.html'>Bleach</a> </center> <h1>this is the title</h1> <h3>SYNOPSIS<h3> <p>blabla</p>; <h3>DESCRIPTION</h3> <p>foo bar</p> <hr> Thu Nov 7 21:51:05 200

DESCRIPTION

new

my $th=Template::YetAnother->new({ template_dir=>'/path/to/templates/', # namespace=>'projectname', # start_tag=>'<--', # end_tag=> '-->', });
Generates a new Template::YetAnother Handler Object.

fill

$th->fill($data);

Fill the template with the data in the data structure.

_gen

my $fragment=$th->_gen('type',$data)

Generates a new Template Fragment

You usually do not have to call this. You just say

$th->type($data)

and AUTOLOAD passes it to "_gen"

SEE ALSO

Search for "template" on http://search.cpan.org, if you dare.
-- #!/usr/bin/perl for(ref bless{},just'another'perl'hacker){s-:+-$"-g&&print$_.$/}

In reply to RFC: Template::YetAnother by domm

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.