| 1 |
NAME |
| 2 |
Tenjin - Fast templating engine with support for embedded Perl. |
| 3 |
|
| 4 |
SYNOPSIS |
| 5 |
use Tenjin; |
| 6 |
|
| 7 |
$Tenjin::USE_STRICT = 1; # use strict in the embedded Perl inside |
| 8 |
# your templates. Recommended, but not used |
| 9 |
# by default. |
| 10 |
|
| 11 |
$Tenjin::ENCODING = "UTF-8"; # set the encoding of your template files |
| 12 |
# to UTF-8. This is the default encoding used |
| 13 |
# so there's no need to do this if your |
| 14 |
# templates really are UTF-8. |
| 15 |
|
| 16 |
my $engine = Tenjin->new(\%options); |
| 17 |
my $context = { title => 'Tenjin Example', items => [qw/AAA BBB CCC/] }; |
| 18 |
my $filename = 'file.html'; |
| 19 |
my $output = $engine->render($filename, $context); |
| 20 |
print $output; |
| 21 |
|
| 22 |
DESCRIPTION |
| 23 |
Tenjin is a very fast and full-featured templating engine, implemented |
| 24 |
in several programming languages, among them Perl. |
| 25 |
|
| 26 |
The Perl version of Tenjin supports embedded Perl code, nestable layout |
| 27 |
template, inclusion of other templates inside a template, capturing |
| 28 |
parts of or the entire template output, file and memory caching, |
| 29 |
template arguments and preprocessing. |
| 30 |
|
| 31 |
The original version of Tenjin is developed by Makoto Kuwata. This CPAN |
| 32 |
version is developed by Ido Perlmuter and differs from the original in a |
| 33 |
few key aspects: |
| 34 |
|
| 35 |
* Code is entirely revised, packages are separated into modules, with |
| 36 |
a smaller number of packages than the original version. In |
| 37 |
particular, the Tenjin::Engine module no longer exists, and is now |
| 38 |
instead just the Tenjin module (i.e. this one). |
| 39 |
|
| 40 |
* Support for rendering templates from non-file sources (such as a |
| 41 |
database) is added. |
| 42 |
|
| 43 |
* Ability to set the encoding of your templates is added (Tenjin will |
| 44 |
decode template files according to this encoding; by default, Tenjin |
| 45 |
will decode |
| 46 |
|
| 47 |
* HTML is encoded and decoded using the HTML::Entities module, instead |
| 48 |
of internally. |
| 49 |
|
| 50 |
* The "pltenjin" script is not provided, at least for now. |
| 51 |
|
| 52 |
To make it clear, the CPAN version of Tenjin might find itself diverting |
| 53 |
a bit in the future from the original Tenjin's roadmap. Although my aim |
| 54 |
is to be as compatible as possible (and this version is always updated |
| 55 |
with features and changes from the original), I cannot guarantee it (but |
| 56 |
I'll do my best). Please note that version 0.05 (and above) of this |
| 57 |
module is NOT backwards compatible with previous versions. |
| 58 |
|
| 59 |
A NOTE ABOUT ENCODING |
| 60 |
When Tenjin opens template files, it will automatically decode their |
| 61 |
contents according to the selected encoding (UTF-8 by default), so make |
| 62 |
sure your template files are properly encoded. Tenjin also writes cache |
| 63 |
files of compiled template structure. These will be automatically |
| 64 |
encoded according to the selected encoding. |
| 65 |
|
| 66 |
When it comes to UTF-8, it might interest you to know how Tenjin |
| 67 |
behaves: |
| 68 |
|
| 69 |
1. "UTF-8" is the default encoding used. If for some reason, either |
| 70 |
before running "Tenjin->new()" or during, you provide an alternate |
| 71 |
spelling (such as "utf8" or "UTF8"), Tenjin will convert it to UTF-8. |
| 72 |
2. When reading files, Tenjin uses "<:encoding(UTF-8)", while when |
| 73 |
writing files, Tenjin uses ">:utf8", as recommended by this article |
| 74 |
<https://secure.wikimedia.org/wikibooks/en/w/index.php?title=Perl_Progra |
| 75 |
mming/Unicode_UTF-8&oldid=2020796>. |
| 76 |
|
| 77 |
METHODS |
| 78 |
new( \%options ) |
| 79 |
This creates a new instant of Tenjin. "\%options" is a hash-ref |
| 80 |
containing Tenjin's configuration options: |
| 81 |
|
| 82 |
* path - Array-ref of filesystem paths where templates will be |
| 83 |
searched |
| 84 |
|
| 85 |
* prefix - A string that will be automatically prepended to template |
| 86 |
names when searching for them in the path. Empty by default. |
| 87 |
|
| 88 |
* postfix - The default extension to be automtically appended to |
| 89 |
template names when searching for them in the path. Don't forget to |
| 90 |
include the dot, such as '.html'. Empty by default. |
| 91 |
|
| 92 |
* cache - If set to 1 (the default), compiled templates will be cached |
| 93 |
on the filesystem (this means the template's code will be cached, |
| 94 |
not the completed rendered output). |
| 95 |
|
| 96 |
* preprocess - Enable template preprocessing (turned off by default). |
| 97 |
Only use if you're actually using any preprocessed Perl code in your |
| 98 |
templates. |
| 99 |
|
| 100 |
* layout - Name of a layout template that can be optionally used. If |
| 101 |
set, templates will be automatically inserted into the layout |
| 102 |
template, in the location where you use "[== $_content ==]". |
| 103 |
|
| 104 |
* strict - Another way to make Tenjin use strict on embedded Perl code |
| 105 |
(turned off by default). |
| 106 |
|
| 107 |
* encoding - Another way to set the encoding of your template files |
| 108 |
(set to "UTF-8" by default). |
| 109 |
|
| 110 |
render( $tmpl_name, [\%_context, $use_layout] ) |
| 111 |
Renders a template whose name is identified by $tmpl_name. Remember that |
| 112 |
a prefix and a postfix might be added if they where set when creating |
| 113 |
the Tenjin instance. |
| 114 |
|
| 115 |
$_context is a hash-ref containing the variables that will be available |
| 116 |
for usage inside the templates. So, for example, if your "\%_context" is |
| 117 |
"{ message => 'Hi there' }", then you can use $message inside your |
| 118 |
templates. |
| 119 |
|
| 120 |
$use_layout is a flag denoting whether or not to render this template |
| 121 |
into a layout template (when doing so, the template will be rendered, |
| 122 |
then the rendered output will be added to the context hash-ref as |
| 123 |
'_content', and finally the layout template will be rendered with the |
| 124 |
revised context and returned. |
| 125 |
|
| 126 |
If $use_layout is 1 (which is the default in case it is undefined), then |
| 127 |
Tenjin will use the layout template that was set when creating the |
| 128 |
Tenjin instance (via the 'layout' configuration option). If you want to |
| 129 |
use a different layout template (or if you haven't defined a layout |
| 130 |
template when creating the Tenjin instance), then you must add the |
| 131 |
layout template's name to the context as '_layout'. You can also just |
| 132 |
pass the layout template's name as $use_layout, but |
| 133 |
"$_context->{_layout}" has precedence. |
| 134 |
|
| 135 |
If $use_layout is 0, then a layout template will not be used, even if |
| 136 |
"$_context->{_layout}" is defined. |
| 137 |
|
| 138 |
Note that you can nest layout templates as much as you like, but the |
| 139 |
only way to do so is by setting the layout template for each template in |
| 140 |
the nesting chain with "$_context->{_layout}". |
| 141 |
|
| 142 |
Please note that by default file templates are cached on disk (with a |
| 143 |
'.cache') extension. Tenjin automatically deprecates these cache files |
| 144 |
every 10 seconds. If you find this value is too low, you can override |
| 145 |
the $Tenjin::TIMESTAMP_INTERVAL variable with your preferred value. |
| 146 |
|
| 147 |
register_template( $template_name, $template ) |
| 148 |
Receives the name of a template and its Tenjin::Template object and |
| 149 |
stores it in memory for usage by the engine. This is useful if you need |
| 150 |
to use templates that are not stored on the file system, for example |
| 151 |
from a database. |
| 152 |
|
| 153 |
Note, however, that you need to pass a template object who's already |
| 154 |
been converted and compiled into Perl code, so if you have a template |
| 155 |
with a certain name and certain text, these are the steps you will need |
| 156 |
to perform: |
| 157 |
|
| 158 |
# create a Tenjin instance |
| 159 |
my $tenjin = Tenjin->new(\%options); |
| 160 |
|
| 161 |
# create an empty template object |
| 162 |
my $template = Tenjin::Template->new(); |
| 163 |
|
| 164 |
# compile template content into Perl code |
| 165 |
$template->convert($tmpl_content); |
| 166 |
$template->compile(); |
| 167 |
|
| 168 |
# register the template with the Tenjin instance |
| 169 |
$tenjin->register_template($tmpl_name, $template); |
| 170 |
|
| 171 |
INTERNAL METHODS |
| 172 |
get_template( $template_name, $_context ) |
| 173 |
Receives the name of a template and the context object and tries to find |
| 174 |
that template in the engine's memory. If it's not there, it will try to |
| 175 |
find it in the file system (the cache file might be loaded, if present). |
| 176 |
Returns the template's Tenjin::Template object. |
| 177 |
|
| 178 |
to_filename( $template_name ) |
| 179 |
Receives a template name and returns the proper file name to be searched |
| 180 |
in the file system, which will only be different than $template_name if |
| 181 |
it begins with ':', in which case the prefix and postfix configuration |
| 182 |
options will be appended and prepended to the template name (minus the |
| 183 |
':'), respectively. |
| 184 |
|
| 185 |
find_template_file( $filename ) |
| 186 |
Receives a template filename and searches for it in the path defined in |
| 187 |
the configuration options (or, if a path was not set, in the current |
| 188 |
working directory). Returns the absolute path to the file. |
| 189 |
|
| 190 |
read_template_file( $template, $filename, $_context ) |
| 191 |
Receives a template object and its absolute file path and reads that |
| 192 |
file. If preprocessing is on, preprocessing will take place using the |
| 193 |
provided context object. |
| 194 |
|
| 195 |
cachename( $filename ) |
| 196 |
Receives a template filename and returns its standard cache filename |
| 197 |
(which will simply be $filename with '.cache' appended to it. |
| 198 |
|
| 199 |
store_cachefile( $cachename, $template ) |
| 200 |
Receives the name of a template cache file and the corresponding |
| 201 |
template object, and creates the cache file on disk. |
| 202 |
|
| 203 |
load_cachefile( $cachename, $template ) |
| 204 |
Receives the name of a template cache file and the corresponding |
| 205 |
template object, reads the cache file and stores it in the template |
| 206 |
object (as 'script'). |
| 207 |
|
| 208 |
create_template( $filename, $_context ) |
| 209 |
Receives an absolute path to a template file and the context object, |
| 210 |
reads the file, processes it (which may involve loading the template's |
| 211 |
cache file or creating the template's cache file), compiles it and |
| 212 |
returns the template object. |
| 213 |
|
| 214 |
SEE ALSO |
| 215 |
The original Tenjin website is located at |
| 216 |
<http://www.kuwata-lab.com/tenjin/>. In there check out |
| 217 |
<http://www.kuwata-lab.com/tenjin/pltenjin-users-guide.html> for |
| 218 |
detailed usage guide, |
| 219 |
<http://www.kuwata-lab.com/tenjin/pltenjin-examples.html> for examples, |
| 220 |
and <http://www.kuwata-lab.com/tenjin/pltenjin-faq.html> for frequently |
| 221 |
asked questions. |
| 222 |
|
| 223 |
Note that the Perl version of Tenjin is referred to as plTenjin on the |
| 224 |
Tenjin website, and that, as opposed to this module, the website |
| 225 |
suggests using a .plhtml extension for the templates instead of .html |
| 226 |
(this is entirely your choice). |
| 227 |
|
| 228 |
Tenjin::Template, Catalyst::View::Tenjin, Dancer::Template::Tenjin. |
| 229 |
|
| 230 |
CHANGES |
| 231 |
Version 0.05 of this module broke backwards compatibility with previous |
| 232 |
versions. In particular, the Tenjin::Engine module does not exist any |
| 233 |
more and is instead integrated into this one. Templates are also |
| 234 |
rendered entirely different (as per changes in the original tenjin) |
| 235 |
which provides much faster rendering. |
| 236 |
|
| 237 |
Upon upgrading to versions 0.05 and above, you MUST perform the |
| 238 |
following changes for your applications (or, if you're using Catalyst, |
| 239 |
you must also upgrade Catalyst::View::Tenjin): |
| 240 |
|
| 241 |
* "use Tenjin" as your normally would, but to get an instance of |
| 242 |
Tenjin you must call "Tenjin->new()" instead of the old method of |
| 243 |
calling "Tenjin::Engine->new()". |
| 244 |
|
| 245 |
* Remove all your templates cache files (they are the '.cache' files |
| 246 |
in your template directories), they are not compatible with the new |
| 247 |
templates structure and WILL cause your application to fail if |
| 248 |
present. |
| 249 |
|
| 250 |
Version 0.06 (this version) restored the layout template feature which |
| 251 |
was accidentally missing in version 0.05, and the ability to call the |
| 252 |
utility methods of Tenjin::Util natively inside templates. You will want |
| 253 |
to remove your templates' .cache files when upgrading to 0.6 too. |
| 254 |
|
| 255 |
AUTHOR |
| 256 |
Ido Perlmuter <ido at ido50.net> |
| 257 |
|
| 258 |
Forked from plTenjin 0.0.2 by Makoto Kuwata |
| 259 |
(<http://www.kuwata-lab.com/tenjin/>). |
| 260 |
|
| 261 |
ACKNOWLEDGEMENTS |
| 262 |
I would like to thank the following people for their contributions: |
| 263 |
|
| 264 |
* Makoto Kuwata |
| 265 |
|
| 266 |
The original developer of Tenjin. |
| 267 |
|
| 268 |
* John Beppu <beppu at cpan.org> |
| 269 |
|
| 270 |
For introducing me to Tenjin and helping me understand the way it's |
| 271 |
designed. |
| 272 |
|
| 273 |
* Pedro Melo <melo at cpan.org> |
| 274 |
|
| 275 |
For helping me understand the logic behind some of the original |
| 276 |
Tenjin aspects and helping me fix bugs and create tests. |
| 277 |
|
| 278 |
BUGS |
| 279 |
Please report any bugs or feature requests on the L<GitHub project page|https://github.com/ido50/Tenjin/issues>. |
| 280 |
|
| 281 |
SUPPORT |
| 282 |
You can find documentation for this module with the perldoc command. |
| 283 |
|
| 284 |
perldoc Tenjin |
| 285 |
|
| 286 |
You can also read the documentation online on metacpan |
| 287 |
<https://metacpan.org/pod/Tenjin>. |
| 288 |
|
| 289 |
LICENSE AND COPYRIGHT |
| 290 |
Tenjin is licensed under the MIT license. |
| 291 |
|
| 292 |
Copyright (c) 2007-2016 the aforementioned authors. |
| 293 |
|
| 294 |
Permission is hereby granted, free of charge, to any person obtaining |
| 295 |
a copy of this software and associated documentation files (the |
| 296 |
"Software"), to deal in the Software without restriction, including |
| 297 |
without limitation the rights to use, copy, modify, merge, publish, |
| 298 |
distribute, sublicense, and/or sell copies of the Software, and to |
| 299 |
permit persons to whom the Software is furnished to do so, subject to |
| 300 |
the following conditions: |
| 301 |
|
| 302 |
The above copyright notice and this permission notice shall be |
| 303 |
included in all copies or substantial portions of the Software. |
| 304 |
|
| 305 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 306 |
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 307 |
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 308 |
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 309 |
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 310 |
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 311 |
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 312 |
|
| 313 |
See http://dev.perl.org/licenses/ for more information. |
| 314 |
|