|
NAMEText::NeatTemplate - a fast, middleweight template engine.VERSIONversion 0.1101SYNOPSISuse Text::NeatTemplate; my $tobj = Text::NeatTemplate->new(); $result = $tobj->fill_in(data_hash=>\%data, show_names=>\%names, template=>$text); DESCRIPTIONThis module provides a simple, middleweight but fast template engine, for when you need speed rather than complex features, yet need more features than simple variable substitution.Markup FormatThe markup format is as follows:
LimitationsTo make the parsing simpler (and therefore faster) there are certain restrictions in what this module can do:
Justification For ExistenceWhen I was writing SQLite::Work, I originally tried using Text::Template (my favourite template engine) and also tried Text::FillIn. Both of them had some lovely, powerful features. Unfortunately, they were also relatively slow. In testing them with a 700-row table, using Text::Template took about 15 seconds to generate the report, and using Text::FillIn took 45 seconds! Rolling my own very simple template engine cut the time down to about 7 seconds.The reasons for this aren't that surprising. Because Text::Template is basically an embedded Perl engine, it has to run the interpreter on each substitution. And Text::FillIn has a lot to do, what with being very generic and very recursive. The trade-off for the speed-gain of Text::NeatTemplate is that it is quite simple. There is no nesting or recursion, there are no loops. But I do think I've managed to grab some of the nicer features of other template engines, such as limited conditionals, and formatting, and, the most powerful of all, calling external functions. This is a middleweight engine rather than a lightweight one, because I needed more than just simple variable substitution, such as one has with Template::Trivial. I consider the trade-off worth it, and others might also, so I made this a separate module. FORMATTINGAs well as simple substitution, this module can apply formatting to values before they are displayed.For example: {$Money:dollars} will give the value of the Money variable formatted as a dollar value. Formatting directives are:
CLASS METHODSnewmy $tobj = Text::NeatTemplate->new();Make a new template object. METHODSfill_inFill in the given values.$result = $tobj->fill_in(data_hash=>\%data, show_names=>\%names, template=>$text); The 'data_hash' is a hash containing names and values. The 'show_names' is a hash saying which of these "variable names" ought to be displayed, and which suppressed. This can be useful if you want to use a more generic template, and then dynamically suppress certain values at runtime. The 'template' is the text of the template. get_varnamesFind variable names inside the given template.@varnames = $tobj->get_varnames(template=>$text); do_replaceReplace the given value.$val = $tobj->do_replace(targ=>$targ, data_hash=>$data_hashref, show_names=>\%show_names); Where 'targ' is the target value, which is either a variable target, or a conditional target. The 'data_hash' is a hash containing names and values. The 'show_names' is a hash saying which of these "variable names" ought to be displayed, and which suppressed. This can do templating by using the exec ability of substitution, for example: $out =~ s/{([^}]+)}/$tobj->do_replace(data_hash=>$data_hash,targ=>$1)/eg; get_value$val = $tobj->get_value(val_id=>$val_id, data_hash=>$data_hashref, show_names=>\%show_names);Get and format the given value. convert_valuemy $val = $tobj->convert_value(value=>$val, format=>$format, name=>$name); Convert a value according to the given formatting directive. See "FORMATTING" for details of all the formatting directives. simple_html$val = $tobj->simple_html($val);Do a simple HTML conversion of the value. bold, italic, <br> Callable Functionssafe_backtick{&safe_backtick(myprog,arg1,arg2...argN)}Return the results of a program, without risking evil shell calls. This requires that the program and the arguments to that program be given separately. format_items{&format_items(fieldname,value,delim,outdelim,format,prefix,suffix)}Format a field made of multiple items. REQUIRESTest::More INSTALLATIONTo install this module, run the following commands:perl Build.PL ./Build ./Build test ./Build install Or, if you're on a platform (like DOS or Windows) that doesn't like the "./" notation, you can do this: perl Build.PL perl Build perl Build test perl Build install In order to install somewhere other than the default, such as in a directory under your home directory, like "/home/fred/perl" go perl Build.PL --install_base /home/fred/perl as the first step instead. This will install the files underneath /home/fred/perl. You will then need to make sure that you alter the PERL5LIB variable to find the module. Therefore you will need to change the PERL5LIB variable to add /home/fred/perl/lib PERL5LIB=/home/fred/perl/lib:${PERL5LIB} SEE ALSOText::Template Text::FillIn Text::QuickTemplate Template::Trivial Template::Toolkit HTML::TemplateBUGSPlease report any bugs or feature requests to the author.AUTHORKathryn Andersen (RUBYKAT) perlkat AT katspace dot com http://www.katspace.org/tools COPYRIGHT AND LICENCECopyright (c) 2006 by Kathryn AndersenThis program 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. |