Skip to content

DavidMcLaughlin/PerlTemplates

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 

Repository files navigation

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 

About

JavaScript parser for HTML::Template templates

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published