Dancer2::Template::TextTemplate::FakeEngine - Fake Template::Toolkit-like, persistent engine around Text::Template.


version 1.003



With Template::Toolkit-like engines, you instantiate one engine to process multiple templates. With Text::Template, you instantiate one engine to process one template. This class is a simple wrapper around Text::Template to simplify its use as a template engine in Dancer2. It basically just manage Text::Template instances (and their expiration) through CHI.

You can give this engine templates as filenames or string references:

  • with a filename, the corresponding file will be read by Text::Template;

  • a string ref will be dereferenced and its content used as the template content itself.



Whether a cache should be used for storing Text::Template instances, or not.

Defaults to 1 (caching enabled).


How longer (in seconds) the a Text::Template instance will stay in the cache. After this duration, when the corresponding template is requested again, a new Text::Template instance will be created (and cached).

Defaults to 3600 (one hour). Obviously irrelevant if caching is set to 0!


An arrayref of (two) delimiters, as defined in Text::Template#new.

Defaults to [ '{', '}' ].


If this attribute and caching are true (which is the default), string-ref-templates will always be cached forever (since they cannot become invalid, contrary to template files that can be changed on disc when FakeEngine doesn't watch).

However, you may want to disable this behavior for string-ref-templates if you use a lot of such templates only once (they would fill your cache). By setting cache_stringrefs to 0, you tell FakeEngine not to cache (at all) your string-ref-templates.


Contains the string of Perl code added at the top of each evaluated template. See PREPEND in Text::Template.

safe, safe_opcodes, safe_disposable

These attributes are directly linked to the eponymous options in Dancer2::Template::TextTemplate.


process( $template, \%tokens )

Computes the $template according to specified \%tokens.

If $template is a string, it is taken as a filename for the template file (if this file does not exist, the method croaks). If $template is a scalar reference, it is taken as a reference to a string that contains the template. In any other case, this method will croak furiously.

Note that, if caching is true, (dereferenced) string references will be cached too.

This methods simply gets a Text::Template instance (either from cache or by instantiating it) and calls Text::Template::fill_in( HASH => \%tokens ) on it, returning the result.

If an error occurs in Text::Template, this method returns undef, sets Dancer2::Template::TextTemplate::FakeEngine::ERROR to the value of Text::Template::ERROR and does not cache anything.

This is our cache. We should gather CHI-related options from config.yml to support other caching methods like Memcached. That sounds overkill to me, but an open interface is always better than a predefined one!


Thibaut Le Page <>


This software is copyright (c) 2014 by Thibaut Le Page.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.