Fast templating engine with support for embedded perl.
version 0.070001
use Tenjin; $Tenjin::USE_STRICT = 1; # use strict in the embedded Perl inside # your templates. Recommended, but not used # by default. $Tenjin::ENCODING = "UTF-8"; # set the encoding of your template files # to UTF-8. This is the default encoding used # so there's no need to do this if your # templates really are UTF-8. my $engine = Tenjin->new(\%options); my $context = { title => 'Tenjin Example', items => [qw/AAA BBB CCC/] }; my $filename = 'file.html'; my $output = $engine->render($filename, $context); print $output;
Tenjin is a very fast and full-featured templating engine, implemented in several programming languages, among them Perl.
The Perl version of Tenjin supports embedded Perl code, nestable layout template, inclusion of other templates inside a template, capturing parts of or the entire template output, file and memory caching, template arguments and preprocessing.
The original version of Tenjin is developed by Makoto Kuwata. This \s-1CPAN\s0 version is developed by Ido Perlmuter and differs from the original in a few key aspects:
Code is entirely revised, packages are separated into modules, with a smaller number of packages than the original version. In particular, the Tenjin::Engine module no longer exists, and is now instead just the Tenjin module (i.e. this one).
Support for rendering templates from non-file sources (such as a database) is added.
Ability to set the encoding of your templates is added (Tenjin will decode template files according to this encoding; by default, Tenjin will decode
\s-1HTML\s0 is encoded and decoded using the HTML::Entities module, instead of internally.
The \*(C`pltenjin\*(C' script is not provided, at least for now.
To make it clear, the \s-1CPAN\s0 version of Tenjin might find itself diverting a bit in the future from the original Tenjin's roadmap. Although my aim is to be as compatible as possible (and this version is always updated with features and changes from the original), I cannot guarantee it (but I'll do my best). Please note that version 0.05 (and above) of this module is \s-1NOT\s0 backwards compatible with previous versions.
When Tenjin opens template files, it will automatically decode their contents according to the selected encoding (\s-1UTF-8\s0 by default), so make sure your template files are properly encoded. Tenjin also writes cache files of compiled template structure. These will be automatically encoded according to the selected encoding.
When it comes to \s-1UTF-8\s0, it might interest you to know how Tenjin behaves:
This creates a new instant of Tenjin. \*(C`\%options\*(C' is a hash-ref containing Tenjin's configuration options:
path - Array-ref of filesystem paths where templates will be searched
prefix - A string that will be automatically prepended to template names when searching for them in the path. Empty by default.
postfix - The default extension to be automatically appended to template names when searching for them in the path. Don't forget to include the dot, such as '.html'. Empty by default.
cache - If set to 1 (the default), compiled templates will be cached on the filesystem (this means the template's code will be cached, not the completed rendered output).
preprocess - Enable template preprocessing (turned off by default). Only use if you're actually using any preprocessed Perl code in your templates.
layout - Name of a layout template that can be optionally used. If set, templates will be automatically inserted into the layout template, in the location where you use \*(C`[== $_content ==]\*(C'.
strict - Another way to make Tenjin use strict on embedded Perl code (turned off by default).
encoding - Another way to set the encoding of your template files (set to \*(L"\s-1UTF-8\s0\*(R" by default). Renders a template whose name is identified by $tmpl_name. Remember that a prefix and a postfix might be added if they where set when creating the Tenjin instance.
$_context is a hash-ref containing the variables that will be available for usage inside the templates. So, for example, if your \*(C`\%_context\*(C' is \*(C`{ message => 'Hi there' }\*(C', then you can use $message inside your templates.
$use_layout is a flag denoting whether or not to render this template into a layout template (when doing so, the template will be rendered, then the rendered output will be added to the context hash-ref as '_content', and finally the layout template will be rendered with the revised context and returned.
If $use_layout is 1 (which is the default in case it is undefined), then Tenjin will use the layout template that was set when creating the Tenjin instance (via the 'layout' configuration option). If you want to use a different layout template (or if you haven't defined a layout template when creating the Tenjin instance), then you must add the layout template's name to the context as '_layout'. You can also just pass the layout template's name as $use_layout, but \*(C`$_context->{_layout}\*(C' has precedence.
If $use_layout is 0, then a layout template will not be used, even if \*(C`$_context->{_layout}\*(C' is defined.
Note that you can nest layout templates as much as you like, but the only way to do so is by setting the layout template for each template in the nesting chain with \*(C`$_context->{_layout}\*(C'.
Please note that by default file templates are cached on disk (with a '.cache') extension. Tenjin automatically deprecates these cache files every 10 seconds. If you find this value is too low, you can override the $Tenjin::TIMESTAMP_INTERVAL variable with your preferred value. Receives the name of a template and its Tenjin::Template object and stores it in memory for usage by the engine. This is useful if you need to use templates that are not stored on the file system, for example from a database.
Note, however, that you need to pass a template object who's already been converted and compiled into Perl code, so if you have a template with a certain name and certain text, these are the steps you will need to perform:
# create a Tenjin instance my $tenjin = Tenjin->new(\%options);
# create an empty template object my $template = Tenjin::Template->new();
# compile template content into Perl code $template->convert($tmpl_content); $template->compile();
# register the template with the Tenjin instance $tenjin->register_template($tmpl_name, $template);
Receives the name of a template and the context object and tries to find that template in the engine's memory. If it's not there, it will try to find it in the file system (the cache file might be loaded, if present). Returns the template's Tenjin::Template object. Receives a template name and returns the proper file name to be searched in the file system, which will only be different than $template_name if it begins with ':', in which case the prefix and postfix configuration options will be appended and prepended to the template name (minus the ':'), respectively. Receives a template filename and searches for it in the path defined in the configuration options (or, if a path was not set, in the current working directory). Returns the absolute path to the file. Receives a template object and its absolute file path and reads that file. If preprocessing is on, preprocessing will take place using the provided context object. Receives a template filename and returns its standard cache filename (which will simply be $filename with '.cache' appended to it. Receives the name of a template cache file and the corrasponding template object, and creates the cache file on disk. Receives the name of a template cache file and the corrasponding template object, reads the cache file and stores it in the template object (as 'script'). Receives an absolute path to a template file and the context object, reads the file, processes it (which may involve loading the template's cache file or creating the template's cache file), compiles it and returns the template object.
The original Tenjin website is located at http://www.kuwata-lab.com/tenjin/ <http://www.kuwata-lab.com/tenjin/>. In there check out http://www.kuwata-lab.com/tenjin/pltenjin-users-guide.html <http://www.kuwata-lab.com/tenjin/pltenjin-users-guide.html> for detailed usage guide, http://www.kuwata-lab.com/tenjin/pltenjin-examples.html <http://www.kuwata-lab.com/tenjin/pltenjin-examples.html> for examples, and http://www.kuwata-lab.com/tenjin/pltenjin-faq.html <http://www.kuwata-lab.com/tenjin/pltenjin-faq.html> for frequently asked questions.
Note that the Perl version of Tenjin is refered to as plTenjin on the Tenjin website, and that, as opposed to this module, the website suggests using a .plhtml extension for the templates instead of .html (this is entirely your choice).
Tenjin::Template, Catalyst::View::Tenjin, Dancer::Template::Tenjin.
Version 0.05 of this module broke backwards compatibility with previous versions. In particular, the Tenjin::Engine module does not exist any more and is instead integrated into this one. Templates are also rendered entirely different (as per changes in the original tenjin) which provides much faster rendering.
Upon upgrading to versions 0.05 and above, you \s-1MUST\s0 perform the following changes for your applications (or, if you're using Catalyst, you must also upgrade Catalyst::View::Tenjin):
\*(C`use Tenjin\*(C' as your normally would, but to get an instance of Tenjin you must call \*(C`Tenjin->new()\*(C' instead of the old method of calling \*(C`Tenjin::Engine->new()\*(C'.
Remove all your templates cache files (they are the '.cache' files in your template directories), they are not compatible with the new templates structure and \s-1WILL\s0 cause your application to fail if present.
Version 0.06 (this version) restored the layout template feature which was accidentally missing in version 0.05, and the ability to call the utility methods of Tenjin::Util natively inside templates. You will want to remove your templates' .cache files when upgrading to 0.6 too.
The \s-1CPAN\s0 version of Tenjin was forked by Ido Perlmuter <ido at ido50.net> from version 0.0.2 of the original plTenjin, which is developed by Makoto Kuwata at http://www.kuwata-lab.com/tenjin/ <http://www.kuwata-lab.com/tenjin/>.
Development of Tenjin is done with github at <http://github.com/ido50/Tenjin>.
I would like to thank the following people for their contributions:
Makoto Kuwata The original developer of Tenjin.
John Beppu <beppu at cpan.org> For introducing me to Tenjin and helping me understand the way it's designed.
Pedro Melo <melo at cpan.org> For helping me understand the logic behind some of the original Tenjin aspects and helping me fix bugs and create tests.
Please report any bugs or feature requests to \*(C`bug-tenjin at rt.cpan.org\*(C', or through the web interface at <http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Tenjin>. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
You can find documentation for this module with the perldoc command.
perldoc Tenjin
You can also look for information at:
\s-1RT:\s0 \s-1CPAN\s0's request tracker <http://rt.cpan.org/NoAuth/Bugs.html?Dist=Tenjin>
AnnoCPAN: Annotated \s-1CPAN\s0 documentation <http://annocpan.org/dist/Tenjin>
\s-1CPAN\s0 Ratings <http://cpanratings.perl.org/d/Tenjin>
Search \s-1CPAN\s0 <http://search.cpan.org/dist/Tenjin/>
Tenjin is licensed under the \s-1MIT\s0 license.
Copyright (c) 2007-2011 the aforementioned authors.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
See http://dev.perl.org/licenses/ for more information.