|
NAMEText::SimpleTemplate - Yet another module for template processing SYNOPSISuse Text::SimpleTemplate; $tmpl = new Text::SimpleTemplate; # create processor object $tmpl->setq(TEXT => "hello, world"); # export data to template $tmpl->load($file); # loads template from named file $tmpl->pack(q{TEXT: <% $TEXT; %>}); # loads template from in-memory data print $tmpl->fill; # prints "TEXT: hello, world" DESCRIPTIONThis is yet another library for template-based text generation.Template-based text generation is a way to separate program code and data, so non-programmer can control final result (like HTML) as desired without tweaking the program code itself. By doing so, jobs like website maintenance is much easier because you can leave program code unchanged even if page redesign was needed. The idea is simple. Whenever a block of text surrounded by '<%' and '%>' (or any pair of delimiters you specify) is found, it will be taken as Perl expression, and will be replaced by its evaluated result. Major goal of this library is simplicity and speed. While there're many modules for template processing, this module has near raw Perl-code (i.e., "s|xxx|xxx|ge") speed, while providing simple-to-use objective interface. INSTALLATION / REQUIREMENTSThis module requires Carp.pm and FileHandle.pm. Since these are standard modules, all you need is perl itself.For installation, standard procedure of perl Makefile.PL make make test make install should work just fine. TEMPLATE SYNTAX AND USAGESuppose you have a following template named "sample.tmpl":=== Module Information === Name: <% $INFO->{Name}; %> Description: <% $INFO->{Description}; %> Author: <% $INFO->{Author}; %> <<% $INFO->{Email}; %>> With the following code... use Safe; use Text::SimpleTemplate; $tmpl = new Text::SimpleTemplate; $tmpl->setq(INFO => { Name => "Text::SimpleTemplate", Description => "Yet another module for template processing", Author => "Taisuke Yamada", Email => "tai\@imasy.or.jp", }); $tmpl->load("sample.tmpl"); print $tmpl->fill(PACKAGE => new Safe); ...you will get following result: === Module Information === Name: Text::SimpleTemplate Description: Yet another module for template processing Author: Taisuke Yamada <tai@imasy.or.jp> As you might have noticed, any scalar data can be exported to template namespace, even hash reference or code reference. By the way, although I used "Safe" module in example above, this is not a requirement. However, if you want to control power of the template editor over program logic, its use is strongly recommended (see Safe for more). DIRECT ACCESS TO TEMPLATE NAMESPACEIn addition to its native interface, you can also access directly to template namespace.$FOO::text = 'hello, world'; @FOO::list = qw(foo bar baz); $tmpl = new Text::SimpleTemplate; $tmpl->pack(q{TEXT: <% $text; %>, LIST: <% "@list"; %>}); print $tmpl->fill(PACKAGE => 'FOO'); While I don't recommend this style, this might be useful if you want to export list, hash, or subroutine directly without using reference. METHODSFollowing methods are currently available.
NOTES / BUGSNested template delimiter will cause this module to fail.CONTACT ADDRESSPlease send any bug reports/comments to <tai@imasy.or.jp>.AUTHORS / CONTRIBUTORS- Taisuke Yamada <tai@imasy.or.jp> - Lin Tianshan <lts@www.qz.fj.cn> COPYRIGHTCopyright 1999-2001. All rights reserved.This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
Visit the GSP FreeBSD Man Page Interface. |