Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

JavaScript parser for HTML::Template templates

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 example
Octocat-spinner-32 README
Octocat-spinner-32 perltemplates.js
Octocat-spinner-32 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
Something went wrong with that request. Please try again.