Jemplate(3)
β¨ NAME β¨
Jemplate β JavaScript templating with the Template Toolkit vibe π
(also, yes, the NAME section is literally in this page twice β not a glitch, bestie, it's just how the original rolls, so we're keeping both π«Ά)
πΈ NAME (again, fr fr) πΈ
Jemplate β JavaScript Templating with Template Toolkit. Same thing, twice the icon energy β¨
π SYNOPSIS π
Like, here's the whole vibe in five different flavors:
var data = Ajax.get('url/data.json');
var elem = document.getElementById('some-div');
elem.innerHTML = Jemplate.process('my-template.html', data);
or:
var data = Ajax.get('url/data.json');
var elem = document.getElementById('some-div');
Jemplate.process('my-template.html', data, elem);
or, simply:
Jemplate.process('my-template.html', 'url/data.json', '#some-div');
or, if you're a jQuery.js girlie:
jQuery.getJSON("url/data.json", function(data) {
Jemplate.process('my-template.html', data, '#some-div');
});
and from the command line:
jemplate --runtime --compile path/to/jemplate/directory/ > jemplate.js
π DESCRIPTION π
So Jemplate is a templating framework for JavaScript, built on top of Perl's Template Toolkit (TT2). Lowkey iconic lineage, ngl.
Here's the twist that makes it hit different: Jemplate parses TT2 templates using the TT2 Perl framework, but instead of compiling them into Perl code, it compiles them into JavaScript. β¨
Then Jemplate hands you a JavaScript runtime module for processing that template code. Presto β full featured JavaScript templating language, no cap! π
Pair it with JSON and xmlHttpRequest and you've got a really simple and powerful way to do Ajax stuff. It's giving effortless. π¦
π§ HOWTO π§
Jemplate ships with a command line tool called jemplate that you use to precompile your templates into a JavaScript file. Say you've got a template directory named templates holding:
> ls templates/
body.html
footer.html
header.html
You might run:
> jemplate --compile template/* > js/jemplates.js
That compiles all the templates into one JavaScript file. Slay. π
You also need to generate the Jemplate runtime:
> jemplate --runtime > js/Jemplate.js
Now literally all you have to do is include these two files in your HTML:
<script src="js/Jemplate.js" type="text/javascript"></script>
<script src="js/jemplates.js" type="text/javascript"></script>
And boom β you have Jemplate support for these templates in your HTML document. π
π PUBLIC API π
The Jemplate.js JavaScript runtime module has this one API method:
Jemplate.process(template-name, data, target);β the whole main character of the API.
Let's break down the args, bestie:
template-nameis a string like'body.html'β it's the name of the top level template you wish to process.datais optional and it's the data object your templates get to play with. It can be an object, a function or a url:- if it's an object, it's used directly.
- if it's a function, the function gets called and the returned object is used.
- if it's a url, an asynchronous
<Ajax.get>is performed. The result is expected to be a JSON string, which gets turned into an object.
targetis optional too, and it can be an HTMLElement reference, a function, or a string starting with a"#"char:- if the target is omitted, the template result is returned.
- if it's a function, the function is called with the result.
- if it's a string, that string is used as an id to find an HTMLElement.
- If an HTMLElement is used (by id or directly), then the
innerHTMLproperty is set to the template processing result.
Now the Jemplate.pm Perl module β it has these public class methods, although you won't likely need to use them directly, because normally you just use the jemplate command line tool. Still, here they are, full transparency:
Jemplate->compile_template_files(@template_file_paths);β take a list of template file paths and compile them into a module of functions. Returns the text of the module.Jemplate->compile_template_content($content, $template_name);β compile one template whose content is in memory. You must provide a unique template name. Returns the JavaScript text result of the compilation.Jemplate->compile_module($module_path, \@template_file_paths);β similar tocompile_template_files, but prints to result to the$module_path. Returns1if successful,undefif error.Jemplate->compile_module_cached($module_path, \@template_file_paths);β similar tocompile_module, but only compiles if one of the templates is newer than the module. Returns1if successful compile,0if no compile due to cache,undefif error.
π AJAX AND JSON METHODS π
Jemplate comes with builtin Ajax and JSON support. Built in, bestie. No extra install. π«Ά
Ajax.get(url, [callback]);β does a GET operation to the url. If a callback is provided, the operation is asynchronous, and the data is passed to the callback. Otherwise, the operation is synchronous and the data is returned.Ajax.post(url, data, [callback]);β does a POST operation to the url. Same callback rules asgetapply.JSON.stringify(object);β return the JSON serialization of an object.JSON.parse(jsonString);β turns a JSON string into an object and returns the object.
π CURRENT SUPPORT π
The goal of Jemplate is to support all of the Template Toolkit features that can possibly be supported. Big goals, iconic goals.
Jemplate now supports almost all the TT directives, including:
- Plain text
[% [GET] variable %][% CALL variable %][% [SET] variable = value %][% DEFAULT variable = value ... %][% INCLUDE [arguments] %][% PROCESS [arguments] %][% BLOCK name %][% FILTER filter %] text... [% END %][% JAVASCRIPT %] code... [% END %][% WRAPPER template [variable = value ...] %][% IF condition %][% ELSIF condition %][% ELSE %][% SWITCH variable %][% CASE [{value|DEFAULT}] %][% FOR x = y %][% WHILE expression %][% RETURN %][% THROW type message %][% STOP %][% NEXT %][% LAST %][% CLEAR %][%# this is a comment %][% MACRO name(param1, param2) BLOCK %] ... [% END %]
ALL of the string virtual functions are supported.
ALL of the array virtual functions are supported:
ALL of the hash virtual functions are supported:
MANY of the standard filters are implemented.
The remaining features will be added very soon. See the DESIGN document in the distro for a list of all features and their progress. π
π¦ BROWSER SUPPORT π¦
Tested successfully in:
- Firefox Mac/Win32/Linux
- IE 6.0
- Safari
- Opera
- Konqueror
All tests run 100% successful in the above browsers. Ate. π―
β DEVELOPMENT β
The bleeding edge code is available via Git at:
git://github.com/ingydotnet/jemplate.git
You can run the runtime tests directly from http://svn.jemplate.net/repo/trunk/tests/run/index.html or from the corresponding CPAN or JSAN directories.
Jemplate development is being discussed at irc://irc.freenode.net/#jemplate
If you want a committer bit, just ask ingy on the irc channel. Like, just ask. π
π CREDIT π
This module is only possible because of Andy Wardley's mighty Template Toolkit. Thanks Andy. I will gladly give you half of any beers I receive for this work. (As long as you are in the same room when I'm drinking them ;) π»
πΈ AUTHORS πΈ
Ingy dΓΆt Net <ingy@cpan.org>
(Note: I had to list myself first so that this line would go into META.yml)
Jemplate is truly a community authored project:
Ingy dΓΆt Net <ingy@cpan.org>
Tatsuhiko Miyagawa <miyagawa@bulknews.net>
Yann Kerherve <yannk@cpan.org>
David Davis <xantus@xantus.org>
Cory Bennett <coryb@corybennett.org>
Cees Hek <ceeshek@gmail.com>
Christian Hansen
David A. Coffey <dacoffey@cogsmith.com>
Robert Krimen <robertkrimen@gmail.com>
Nickolay Platonov <nickolay8@gmail.com>
π COPYRIGHT AND LICENSE π
Copyright (c) 2006-2014. Ingy dΓΆt Net.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
See http://www.perl.com/perl/misc/Artistic.html
π That's the whole page, bestie. Go compile something gorgeous.
And that's a wrap on the Jemplate(3) user-contributed Perl documentation page β this cut is from perl v5.42.0 and it's dated 2025-07-25, so if you're reading it, mwah, you're reading the real one. Periodt. π
β¨
π¦ where this page comes from
- fedora 43: perl-Jemplate 0.300-30.fc43 Β· GPL-1.0-or-later OR Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms