Perl interface to Django-inspired Swig templating engine.
my $swig = Template::Swig->new;
# Compile and render an inline template:
$swig->compile('message', 'Welcome, {{name}}');
my $output = $swig->render('message', { name => 'Arthur' });
# Compile and render a file:
$swig->compileFromFile('path/to/file.html');
my $output = $swig->render('path/to/file.html', { some_param => 'foo' });
Template::Swig uses JavaScript::V8 and Paul Armstrong's Swig templating engine to provide fast Django-inspired templating in a Perl context. Templates are compiled to JavaScript functions and stored in memory, then executed each time they're rendered.
Swig's feature list includes multiple inheritance, formatter and helper functions, macros, auto-escaping, and custom tags. See the Swig Documentation for more.
Initialize a swig instance, given the follwing parameters:
Optional path where templates live
Optional callback to be run when Swig encounters an
extends
tag; receives filename and its encoding as parameters
Compile a template given, given a template name and swig template source as a string.
Render a template, given a name and a reference to a hash of data to interpolate.
Iterate through a list:
{% for image in images %}
<img src="{{ image.src }}" width="{{ image.width }}" height="{{ image.height }}">
{% else %}
<div class="message">No images to show</div>
{% endfor %}
Custom helpers / filters:
{{ created|date('r') }}
In main.html:
{% block greeting %}
Hi, there.
{% endblock %}
In custom.html:
{% extends 'main.html' %}
{% block greeting %}
Welcome, {{ name }}
{% endblock %}
Dotiac::DTL, Text::Caml, Template::Toolkit
Copyright (c) 2012, David Chester
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.