NAME
JavaScript::V8::Handlebars - Compile and execute Handlebars templates via the actual JS library
SYNOPSIS
use JavaScript::V8::Handlebars;
#use JavaScript::V8::Handlebars ( library_path => "/path/to/handlebars.js" );
my $hbjs = JavaScript::V8::Handlebars->new;
print $hbjs->render_string( "Hello {{var}}", { var => "world" } );
my $template = $hbjs->compile_file( "template.hbs" );
print $template->({ var => "world" });
$hbjs->add_template_dir( './templates' );
open my $oh, ">", "template.bundle.js" or die $!;
print $oh $hbjs->bundle;
close $oh;
METHODS
Package Methods
- use JavaScript::V8::Handlebars ( [library_path => "/path/to/handlebars.js"] );
-
When
use
ing the library you may pass an optional path to a (full) handlebars.js file to use instead of the one it comes bundled with. - JavaScript::V8::Handlebars->handlebars_path
-
Returns the path to the handlebars.js file, set at the package level. Note that this may be overridden on a per object basis and can be changed after an object is created.
- JavaScript::V8::Handlebars->handlebars_code
-
Returns the complete source of the handlebars.js file specified as above.
Object Methods
- $hbjs->new(%opts)
-
Arguments:
- $hbjs->c()
-
Returns the internal JavaScript::V8 object, useful for executing javascript code in the context of the module.
- $hbjs->eval_file($javascript_filename)
- $hbjs->eval($javascript_string)
-
Wrapper function for
$hbjs-
c->eval> that checks for errors and throws an exception. - $hbjs->precompile_file($template_filename)
- $hbjs->precompile($template_string)
-
Takes a template and translates it into the javascript code suitable for passing to the
template
method. See handlebar.js for specifics. - $hbjs->compile_file($template_filename)
- $hbjs->compile($template_string)
-
Takes a template and returns a subref that takes a hashref containing variables as an argument and returns the text of the executed template.
- $hbjs->register_helper( $name, $js_code | $coderef )
-
Takes a name to store the helper under as well as either a perl code reference or a string of javascript to be compiled. These helpers can then be referred to from other templates via the standard Handlebars syntax.
- $hbjs->template( $compiled_javascript_string | $compiled_perl_object )
-
Takes a precompiled template datastructure and returns a subref ready to be executed.
- $hbjs->render_string( $template_string, \%context_vars )
-
Wrapper method for compiling and then executing a template passed as a string.
- $hbjs->add_template_dir( $directory, [$extension] )
-
Recurses through a specified directory looking for each file that matches .$extension, which defaults to hbs. For each file it finds it calls add_template_file with a name based on the path relative to the template $directory Ex. "templates/foo/bar.hbs" is stored under the name as "foo/bar"
If the file found inside a directory named 'partial(s)' then registered_partial_file is called instead with a name derived in the same way as described above.
- $hbjs->add_template_file( $filename, [$name] )
-
Compiles and caches the specified filename so it's available for later execution or bundling. Takes an optional $name argument which specifies the name to internally store the template as, if omitted the name is set to the filename portion of the path with any extension removed.
- $hbjs->add_template( $name, $template_string )
-
Takes a template, compiles it and adds it to the internal store of cached templates for
execute_template
to use. - $hbjs->execute_template( $name, \%context_vars )
-
Executes a cached template.
- $hbjs->bundle()
-
Returns a string of javascript consisting of all the templates in the cache ready for execution by the browser.
- $hbjs->safeString($string)
-
Whatever the original Handlebar function does.
- $hbjs->escapeString ($string)
-
Whatever the original Handlebar function does.
- $hbjs->escape_expression ($string)
-
Whatever the original Handlebar function does.
- $hbjs->register_partial_file($name, $filename)
-
Registers a partial with name $name from a file named $filename
- $hbjs->register_partial($name, $template_string)
-
Registers a partial named $name with the code in $template_string and makes it globally available to templates.
AUTHOR
Robert Grimes, <rmzgrimes at gmail.com>
BUGS
Please report and bugs or feature requests through the interfaces at https://github.com/rmzg/JavaScript-V8-Handlebars
SUPPORT
You can find documentation for this module with the perldoc command.
perldoc JavaScript::V8::Handlebars
LICENSE AND COPYRIGHT
Copyright 2015 Robert Grimes.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
SEE ALSO
https://github.com/rmzg/JavaScript-V8-Handlebars, http://handlebarsjs.com/