Jemplate
view release on metacpan or search on metacpan
lib/Jemplate.pm view on Meta::CPAN
close MODULE;
return 1;
}
sub compile_module_cached {
my ($self, $module_path, $template_file_paths) = @_;
my $m = -M $module_path;
return 0 unless grep { -M($_) < $m } @$template_file_paths;
return $self->compile_module($module_path, $template_file_paths);
}
sub compile_template_files {
my $self = shift;
my $output = $self->_preamble;
for my $filepath (@_) {
my $filename = $filepath;
$filename =~ s/.*[\/\\]//;
open FILE, $filepath
or die "Can't open '$filepath' for input:\n$!";
my $template_input = do {local $/; <FILE>};
close FILE;
$output .=
$self->compile_template_content($template_input, $filename);
}
return $output;
}
sub compile_template_content {
die "Invalid arguments in call to Jemplate->compile_template_content"
unless @_ == 3;
my ($self, $template_content, $template_name) = @_;
my $parser = Jemplate::Parser->new( ref($self) ? %$self : () );
my $parse_tree = $parser->parse(
$template_content, {name => $template_name}
) or die $parser->error;
my $output =
"Jemplate.templateMap['$template_name'] = " .
$parse_tree->{BLOCK} .
"\n";
for my $function_name (sort keys %{$parse_tree->{DEFBLOCKS}}) {
$output .=
"Jemplate.templateMap['$function_name'] = " .
$parse_tree->{DEFBLOCKS}{$function_name} .
"\n";
}
return $output;
}
sub _preamble {
return <<'...';
/*
This JavaScript code was generated by Jemplate, the JavaScript
Template Toolkit. Any changes made to this file will be lost the next
time the templates are compiled.
Copyright 2006-2014 - Ingy döt Net - All rights reserved.
*/
var Jemplate;
if (typeof(exports) == 'object') {
Jemplate = require("jemplate").Jemplate;
}
if (typeof(Jemplate) == 'undefined')
throw('Jemplate.js must be loaded before any Jemplate template files');
...
}
1;
__END__
=encoding utf8
=head1 NAME
Jemplate - JavaScript Templating with Template Toolkit
=head1 NAME
Jemplate - JavaScript Templating with Template Toolkit
=head1 SYNOPSIS
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, with jQuery.js:
jQuery.getJSON("url/data.json", function(data) {
Jemplate.process('my-template.html', data, '#some-div');
});
From the commandline:
jemplate --runtime --compile path/to/jemplate/directory/ > jemplate.js
=head1 DESCRIPTION
Jemplate is a templating framework for JavaScript that is built over
Perl's Template Toolkit (TT2).
Jemplate parses TT2 templates using the TT2 Perl framework, but with a
twist. Instead of compiling the templates into Perl code, it compiles
them into JavaScript.
Jemplate then provides a JavaScript runtime module for processing
the template code. Presto, we have full featured JavaScript
templating language!
( run in 2.397 seconds using v1.01-cache-2.11-cpan-99c4e6809bf )