DavidMcLaughlin / PerlTemplates
- Source
- Commits
- Network (0)
- Issues (0)
- Downloads (0)
- Wiki (1)
- Graphs
-
Tree:
490298a
commit 490298ab95f11aad6169ecc893ff5d48f68c2e94
tree 86b2009bdc57f8b78d2cb260fa0f52ecc2c38c9b
parent bf57c8a6b61b5ff4eeb930460efab1168656baef
tree 86b2009bdc57f8b78d2cb260fa0f52ecc2c38c9b
parent bf57c8a6b61b5ff4eeb930460efab1168656baef
| name | age | message | |
|---|---|---|---|
| |
README | ||
| |
example/ | ||
| |
perltemplates.js | ||
| |
perltemplates.min.js |
README
PerlTemplates
Allows you to reuse your HTML::Template templates in your JavaScript code.
Motivation
I originally wrote this code to facilitate the principles of DRY and
progressive enhancement in our AJAX applications.
At first I adopted the EJS library(www.embeddedjs.com) which was aimed
at Ruby on Rails developers, but found myself constantly rewriting
my HTML::Template files to fit the Rails template syntax. Changes in
the markup or presentation logic in one template had to be duplicated
across our two templates.
Using PerlTemplates, we only to keep one template. It has also
simplified the AJAX handling logic in our perl scripts. We can take code
that looks like this:
my $template = HTML::Template->new('/path/to/search/results.tmpl');
$template->param(%values); # where values contains the template data structure
print CGI->header, $template->output();
Into this:
if($ajax_request)
{
print CGI->header, JSON->new->encode(%values);
}
else
{
my $template = HTML::Template->new('/path/to/search/results.tmpl');
$template->param(%values);
print CGI->header, $template->output();
}
And the JavaScript to support this (with jQuery) becomes:
$.getJSON('/search/results', function(json_data) {
var tmpl = new PerlTemplates({url:'results.tmpl', data: json_data, target: 'search-results'});
tmpl.render();
}
And now we have a both an AJAX and non-JavaScript version of our search results script.
Caveats
* Currently, most of the configuration options available to HTML::Template constructor
are NOT supported.
* Your templates must be accessible over HTTP.
Credits
The concept of converting a template into a 'process' function
was taken from the EmbeddedJS library at www.embeddedjs.com 
