Template::Flute - Modern designer-friendly HTML templating Engine
Version 0.0180
use Template::Flute; my ($cart, $flute, %values); $cart = [{...},{...}]; $values{cost} = ... $flute = new Template::Flute(specification_file => 'cart.xml', template_file => 'cart.html', iterators => {cart => $cart}, values => \%values, autodetect => { disable => [qw/Foo::Bar/], } ); print $flute->process();
Template::Flute enables you to completely separate web design and programming tasks for dynamic web applications.
Templates are designed to be designer-friendly; there's no inline code or mini templating language for your designers to learn - instead, standard HTML and CSS classes are used, leading to HTML that can easily be understood and edited by WYSIWYG editors and hand-coding designers alike.
An example is easier than a wordy description:
Given the following template snippet:
<div class="customer_name">Mr A Test</div> <div class="customer_email">someone@example.com</div>
and the following specification:
<specification name="example" description="Example"> <value name="customer_name" /> <value name="email" class="customer_email" /> </specification>
Processing the above as follows:
$flute = Template::Flute->new( template_file => 'template.html', specification_file => 'spec.xml', ); $flute->set_values({ customer_name => 'Bob McTest', email => 'bob@example.com', });; print $flute->process;
The resulting output would be:
<div class="customer_name">Bob McTest</div> <div class="email">bob@example.com</div>
In other words, rather than including a templating language within your templates which your designers must master and which could interfere with previews in WYSWYG tools, CSS selectors in the template are tied to your data structures or objects by a specification provided by the programmer.
The easiest way to use Template::Flute is to pass all necessary parameters to the constructor and call the process method to generate the HTML.
You can also break it down in separate steps:
Parse specification based on your specification format (e.g with Template::Flute::Specification::XML or Template::Flute::Specification::Scoped.).
$xml_spec = new Template::Flute::Specification::XML; $spec = $xml_spec->parse(q{<specification name="cart" description="Cart"> <list name="cart" class="cartitem" iterator="cart"> <param name="name" field="title"/> <param name="quantity"/> <param name="price"/> </list> <value name="cost"/> </specification>});
Parse template with Template::Flute::HTML object.
$template = new Template::Flute::HTML; $template->parse(q{<html> <head> <title>Cart Example</title> </head> <body> <table class="cart"> <tr class="cartheader"> <th>Name</th> <th>Quantity</th> <th>Price</th> </tr> <tr class="cartitem"> <td class="name">Sample Book</td> <td><input class="quantity" name="quantity" size="3" value="10"></td> <td class="price">$1</td> </tr> <tr class="cartheader"><th colspan="2"></th><th>Total</th> </tr> <tr> <td colspan="2"></td><td class="cost">$10</td> </tr> </table> </body></html>}, $spec);
$flute = new Template::Flute(template => $template, iterators => {cart => $cart}, values => {cost => '84.94'}); $flute->process();
Create a Template::Flute object with the following parameters:
Specification file name.
Select specification parser. This can be either the full class name like MyApp::Specification::Parser or the last part for classes residing in the Template::Flute::Specification namespace.
Specification object or specification as string.
HTML template file.
Template::Flute::HTML object or template as string.
Template::Flute::Database::Rose object.
Hash reference of filter functions.
Template::Flute::I18N object.
An arrayref of attribute names to translate. If the name has a dot, it is interpreted as tagname + attribute, so placeholder" will unconditionally translate all the placeholders, while input.placeholder only the placeholder found on the input tag.
placeholder
input.placeholder
Additional dotted values compose conditions for attributes. E.g. input.value.type.submit means all the value attributes with attribute type set to submit.
input.value.type.submit
type
submit
Defaults to ['input.value.type.submit', 'placeholder']
['input.value.type.submit', 'placeholder']
Hash references of iterators.
Hash reference of values to be used by the process method.
Builds iterators automatically from values.
A configuration option. It should be an hashref with a key disable and a value with an arrayref with a list of classes for objects which should be considered plain hashrefs instead. Example:
disable
my $flute = Template::Flute->new(.... autodetect => { disable => [qw/My::Object/] }, .... );
Doing so, if you pass a value holding a My::Object object, and you have a specification with something like this:
My::Object
<specification> <value name="name" field="object.method"/> </specification>
The value will be $object-{method}>, not $object-$method>.
$object-
The object is checked with isa.
isa
Classical example: Dancer::Session::Abstract.
Dancer::Session::Abstract
Base URI for your template. This adjusts the links in the HTML tags a, base, img, link and script.
a
base
img
link
script
This is meant to be used on HTML emails. When this is set to an hash reference (which should be empty), the hash will be populated with the following values:
cid1 => { filename => 'foo.png' }, cid2 => { filename => 'foo2.gif' },
and in the body the images src attribute will be replaced with cid:cid1.
src
cid:cid1
The cid names are arbitrary and assigned by the template. The code should look at the reference values which were modified.
Optional hashref with options for the CID replacement behaviour.
By default, if the source looks like an HTTP/HTTPS URI, the image source is not altered and no CID is assigned.
If you pass a base_url value in this hashref, the URI matching it will be converted to cids and the rest of the path will be added to the email_cids hashref.
base_url
email_cids
Example:
my $cids = {}; $flute = Template::Flute->new(template => $template, specification => $spec, email_cids => $cids, cids => { base_url => 'http://example.com/' });
Say the template contains images with source http://example.com/image.png, the email_cids hashref will contain a cid with filename "image.png".
http://example.com/image.png
filename
Processes HTML template, manipulates the HTML tree based on the specification, values and iterators.
Returns HTML output.
Processes HTML template and returns Template::Flute::HTML object.
Runs the filter used by ELEMENT on VALUE and returns the result.
Returns the value for NAME.
Sets hash reference of values to be used by the process method. Same as passing the hash reference as values argument to the constructor.
Returns HTML template object, see Template::Flute::HTML for details.
Returns specification object, see Template::Flute::Specification for details.
The specification ties the elements in the HTML template to the data (variables, lists, forms) which is added to the template.
The default format for the specification is XML implemented by the Template::Flute::Specification::XML module. You can use the Config::Scoped format implemented by Template::Flute::Specification::Scoped module or write your own specification parser class.
Common attributes for specification elements are:
Name of element.
<value name="dancefloor"/>
Class of corresponding elements in the HTML template.
<value name="dancefloor" class="dancefloor-link"/>
If this attribute is omitted, the value of the name attribute is used to relate to the class in the HTML template.
Id of corresponding element in the HTML template. Overrides the class attribute for the specification element.
<value name="dancefloor" id="dancefloor-link"/>
HTML attribute to fill the value instead of replacing the body of the HTML element.
<value name="dancefloor" class="dancefloor-link" target="href"/>
Possible elements in the specification are:
The first container is only shown in the output if the value billing_address is set:
billing_address
<container name="billing" value="billing_address" class="billingWrapper"> </container>
The second container is shown if the value warnings or the value errors is set:
warnings
errors
<container name="account_errors" value="warnings|errors" class="infobox"> <value name="warnings"/> <value name="errors"/> </container>
Separator elements for list are added after any list item in the output with the exception of the last one.
Example specification, HTML template and output:
<specification> <list name="list" iterator="tokens"> <param name="key"/> <separator name="sep"/> </list> </specification> <div class="list"><span class="key">KEY</span></div><span class="sep"> | </span> <div class="list"><span class="key">FOO</span></div><span class="sep"> | </span> <div class="list"><span class="key">BAR</span></div>
Param elements are replaced with the corresponding value from the list iterator.
The following operations are supported for param elements:
Appends the param value to the text found in the HTML template.
The attribute to operate on. See below target for value for details.
target
value
When the args attribute is set to tree, it doesn't interpolate anything and just shows corresponding HTML element if param value is set.
args
tree
With target attribute, it simply toggles the target attribute.
Otherwise, if value is true, shows the HTML element and set its content to the value. If value is false, removes the HTML element.
So, if your element has children elements, you probably want to use the args="tree" attribute (see below for an example).
args="tree"
Other attributes for param elements are:
Applies filter to param value.
Uses value from increment instead of a value from the iterator.
<param name="pos" increment="1">
Value elements are replaced with a single value present in the values hash passed to the constructor of this class or later set with the set_values method.
The following operations are supported for value elements:
Appends the value to the text found in the HTML template.
Insert HTML residing in value as subtree of the corresponding HTML element. HTML will be parsed with XML::Twig. See "INSERT HTML" for an example.
Only shows corresponding HTML element if value is set.
Other attributes for value elements are:
Specify the attribute to operate on instead of the tag content. It can be a named attribute (e.g., href), the wildcard character(*, meaning all the attributes found in the HTML template), or a comma separated list (e.g., alt,title).
href
*
alt,title
Applies filter to value.
Processes the template file named in this attribute. This implies the hook operation. See "INCLUDE FILES" for more information.
Form elements are tied through specification to HTML forms. Attributes for form elements in addition to class and id are:
class
id
The link attribute can only have the value name and allows to base the relationship between form specification elements and HTML form tags on the name HTML attribute instead of class, which is usually more convenient.
name
This attribute (which can be provided to param or value specification elements) supports the following values:
param
Do not replace the template string if the value or parameter is undefined, empty or just whitespace.
E.g.
<value name="cartline" skip="empty"/> <list name="items" iterator="items"> <param name="category" skip="empty"/> </list>
You can define patterns in your specification to interpolate the strings instead of replacing them.
A pattern is defined by the attributes name and type and its content. type can be only string or regexp.
string
regexp
The interpolation happens if the value and param elements of the specification have an attribute pattern set with the pattern's name.
pattern
Given this HTML:
<p class="cartline">There are 123 items in your shopping cart.</p> <ul> <li class="items"> <span class="number">1</span> <span class="category">in category 123</span> </li> </ul>
And this specification:
<specification> <pattern name="pxt" type="string">123</pattern> <list name="items" iterator="items"> <param name="number"/> <param name="category" pattern="pxt"/> </list> <value name="cartline" pattern="pxt"/> </specification>
In this example, in the cartline and category classes' text, only the template text "123" will be replaced by the value, not the whole element content, yielding such output:
<p class="cartline">There are 42 items in your shopping cart.</p> <ul> <li class="items"> <span class="number">1</span> <span class="category">in category tofu</span> </li> <li class="items"> <span class="number">2</span> <span class="category">in category pizza</span> </li> </ul>
Appends the value to the text inside a HTML element or to an attribute if target has been specified. This can be used in value and param specification elements.
The example shows how to add a HTML class to elements in a list:
HTML:
<ul class="nav-sub"> <li class="category"><a href="" class="catname">Medicine</a></li> </ul>
XML:
<specification> <list name="category" iterator="categories"> <param name="name" class="catname"/> <param name="catname" field="uri" target="href"/> <param name="css" class="catname" target="class" op="append" joiner=" "/> </list> </specification>
Conditional processing like IF or ELSE is done with the help of containers.
IF
ELSE
In this example we want to show an image only on a certain condition:
<span class="banner-box"> <img class="banner" src=""/> </span>
<container name="banner-box" value="banner"> <value name="banner" target="src"/> </container>
Source code:
if ($organization eq 'Big One') { $values{banner} = 'banners/big_one.png'; }
In this example we want so show a link only if an URL is available:
<div class="linklist"> <span class="name">Name</span> <div class="link"> <a href="#" class="url">Goto ...</a> </div> </div>
<specification name="link"> <list name="links" class="linklist" iterator="links"> <param name="name"/> <param name="url" target="href"/> <container name="link" class="link" value="url"/> </list> </specification>
@records = ({name => 'Link', url => 'http://localhost/'}, {name => 'No Link'}, {name => 'Another Link', url => 'http://localhost/'}, ); $flute = Template::Flute->new(specification => $spec_xml, template => $template, iterators => {links => \@records}); $output = $flute->process();
Template::Flute uses iterators to retrieve list elements and insert them into the document tree. This abstraction relieves us from worrying about where the data actually comes from. We basically just need an array of hash references and an iterator class with a next and a count method. For your convenience you can create an iterator from Template::Flute::Iterator class very easily.
Iterators can be used for dropdowns (HTML <select> elements) as well.
Template:
<select class="color"></select>
Specification:
<value name="color" iterator="colors"/>
Code:
@colors = ({value => 'red', label => 'Red'}, {value => 'black', label => 'Black'}); $flute = Template::Flute->new(template => $html, specification => $spec, iterators => {colors => \@colors}, values => {color => 'black'}, );
HTML output:
<select class="color"> <option value="red">Red</option> <option value="black" selected="selected">Black</option> </select>
You can specify the dropdown item which is selected by default with the iterator_default) attribute.
iterator_default
<value name="color" iterator="colors" iterator_default="black"/>
@colors = ({value => 'red', label => 'Red'}, {value => 'black', label => 'Black'}); $flute = Template::Flute->new(template => $html, specification => $spec, iterators => {colors => \@colors}, );
By default, the iterator for a dropdown is an arrayref of hashrefs with two hardcoded keys: value and (optionally) label. You can override this behaviour in the specification with iterator_value_key and iterator_name_key to use your own hashref's keys from the iterator, instead of value and label.
label
iterator_value_key
iterator_name_key
<specification> <value name="color" iterator="colors" iterator_value_key="code" iterator_name_key="name"/> </specification>
<html> <select class="color"> <option value="example">Example</option> </select> </html>
@colors = ({code => 'red', name => 'Red'}, {code => 'black', name => 'Black'}, ); $flute = Template::Flute->new(template => $html, specification => $spec, iterators => {colors => \@colors}, values => { color => 'black' }, ); $out = $flute->process();
Output:
<html> <head></head> <body> <select class="color"> <option value="red">Red</option> <option selected="selected" value="black">Black</option> </select> </body> </html>
Sometimes you may wish to limit the number or iterations through you list.
<specification> <list name="images" iterator="images" limit="1"> <param name="image" target="src" field="image_url" /> </list> </specification>
<div class="images"> <img class="image" src="/images/bottle.jpg" /> </div>
$images = [ { image_url => '/images/bottle1.jpg' }, { image_url => '/images/bottle2.jpg' }, { image_url => '/images/bottle3.jpg' }, ]; $flute = Template::Flute->new( template => $html, specification => $spec, values => { images => $images }, ); $out = $flute->process;
<html><head></head><body> <div class="images"> <img class="image" src="/images/bottle1.jpg" /> </div> </body></html>
Lists can be accessed after parsing the specification and the HTML template through the HTML template object:
$flute->template->lists(); $flute->template->list('cart');
Only lists present in the specification and the HTML template can be addressed in this way.
See Template::Flute::List for details about lists.
You can pass objects and hashrefs as values. To access a key or an accessor, you have to use a dotted notation with field. An example for both hashrefs and objects follows.
field
<specification> <value name="object" field="myobject.method" /> <value name="struct" field="mystruct.key" /> </specification>
<html> <body> <span class="object">Welcome back!</span> <span class="struct">Another one</span> </body> </html>
package My::Object; sub new { my $class = shift; bless {}, $class; } sub method { return "Hello from the method"; } package main; my $flute = Template::Flute->new( specification => $spec, template => $html, values => { myobject => My::Object->new, mystruct => { key => "Hello from hash" }, } );
process will return:
process
<html> <head></head> <body> <span class="object">Hello from the method</span> <span class="struct">Hello from hash</span> </body> </html>
Sometimes you need to treat an object like an hashref. How to do that is explained under the autodetect option for the constructor.
autodetect
Forms can be accessed after parsing the specification and the HTML template through the HTML template object:
$flute->template->forms(); $flute->template->form('edit_content');
Only forms present in the specification and the HTML template can be addressed in this way.
See Template::Flute::Form for details about forms.
Filters are used to change the display of value and param elements in the resulting HTML output:
<value name="billing_address" filter="eol"/> <param name="price" filter="currency"/>
The following filters are included:
Uppercase filter, see Template::Flute::Filter::Upper.
Strips whitespace at the beginning at the end, see Template::Flute::Filter::Strip.
Filter preserving line breaks, see Template::Flute::Filter::Eol.
Filter replacing missing text with no-break space, see Template::Flute::Filter::NobreakSingle.
Currency filter, see Template::Flute::Filter::Currency. Requires Number::Format module.
Date filter, see Template::Flute::Filter::Date. Requires DateTime and DateTime::Format::ISO8601 modules.
Country name filter, see Template::Flute::Filter::CountryName. Requires Locales module.
Language name filter, see Template::Flute::Filter::LanguageName. Requires Locales module.
JSON to Javascript variable filter, see Template::Flute::Filter::JsonVar. Requires JSON module.
Replaces spaces with dashes (-) and makes lowercase. see Template::Flute::Filter::LowerDash.
Filter classes are loaded at runtime for efficiency and to keep the number of dependencies for Template::Flute as small as possible.
See above for prerequisites needed by the included filter classes.
Filters can also be chained:
<value name="note" filter="upper eol"/>
Example template:
<div class="note"> This is a note. </div>
With the following value:
Update now! Avoid security hazards!
The HTML output would look like:
<div class="note"> UPDATE NOW!<br /> AVOID SECURITY HAZARDS! </div>
HTML can be generated in the code or retrieved from a database and inserted into the template through the hook operation:
hook
<value name="description" op="hook"/>
The result replaces the inner HTML of the following div tag:
div
<div class="description"> Sample content </div>
Files, especially components for web pages can be processed and included through value elements with the include attribute:
<value name="sidebar" include="component.html"/>
<div class="sidebar"> Sample content </div>
Stefan Hornburg (Racke), <racke@linuxia.de>
Please report any bugs or feature requests at https://github.com/racke/Template-Flute/issues.
You can find documentation for this module with the perldoc command.
perldoc Template::Flute
You can also look for information at:
AnnoCPAN: Annotated CPAN documentation
http://annocpan.org/dist/Template-Flute
CPAN Ratings
http://cpanratings.perl.org/d/Template-Flute
Search CPAN
http://search.cpan.org/dist/Template-Flute/
Thanks to Peter Mottram (GH #81, #87).
Thanks to William Carr (GH #86, #91).
Thanks to David Precious (bigpresh) for writing a much clearer introduction for Template::Flute.
Thanks to Grega Pompe for proper implementation of nested lists and a documentation fix.
Thanks to Jeff Boes for spotting a typo in the documentation of the Template::Flute::Filter::JsonVar class.
Thanks to Ton Verhagen for being a big supporter of my projects in all aspects.
Thanks to Sam Batschelet (GH #14, #93).
Thanks to Terrence Brannon for spotting a documentation mix-up.
Template::Flute was initially named Template::Zoom. I renamed the module because of a request from Matt S. Trout, author of the HTML::Zoom module.
Copyright 2010-2014 Stefan Hornburg (Racke) <racke@linuxia.de>.
This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.
See http://dev.perl.org/licenses/ for more information.
To install Template::Flute, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Template::Flute
CPAN shell
perl -MCPAN -e shell install Template::Flute
For more information on module installation, please visit the detailed CPAN module installation guide.