-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREADME
60 lines (40 loc) · 1.97 KB
/
README
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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