Perl: Javascript::V8 templates - from the perl

女生的网名这么多〃 提交于 2019-12-03 03:15:16

Revisited and edited after years :)

Here is the EJS::Template. It does exactly for what you asked - compiles the templates into JS and using V8 (or even JE) engines for evaluate. Unfortunately, has no Javascript::Duktape engine support (yet).

Also, here is a snipet how to use the Jemplate (server-side) from the great @ysth's answer with the Duktape engine.

use strict;
use warnings;

use Jemplate;
use JavaScript::Duktape;

# can omit these steps - see bellow 
# Get the lite runtime js-source without the unnecessary AJAX  (we are server side)
my $jemp_runtime = Jemplate::runtime_source_code('lite');

# The Template::Toolkit template
my $template = q{
[%- FOREACH pope IN perlmonks -%]
pope: [% pope.name %] = [% pope.experience %]
[% END -%]
};

# compile the Template source using Jemplate and name it
my $jemp_template = Jemplate->compile_template_content($template, 'monkstemplate');

# the data
my $data = {
    'perlmonks' => [
        { 'name' => 'vroom',    'experience' => '1007479', },
        { 'name' => 'BrowserUk','experience' => '167247', },
        { 'name' => 'Corion',   'experience' => '133975', },
        { 'name' => 'ikegami',  'experience' => '128977', }
    ]
};

# init
my $js = JavaScript::Duktape->new();
$js->set( 'write' => sub { print $_[0]; } );
$js->eval($jemp_runtime);   # eval the runtime code
$js->eval($jemp_template);  # the Template code compiled into JS
$js->set("monkdata", $data);# bind the data

# finally eval the template processing code
$js->eval(q!
    write(
        Jemplate.process('monkstemplate', monkdata)
    );
!);

produces

pope: vroom = 1007479
pope: BrowserUk = 167247
pope: Corion = 133975
pope: ikegami = 128977 

You can omit all Jemplate calls, by compiling the templates beforehand using the jemplate command, like:

jemplate --runtime=lite --compile /path/to/templates > jemplate_source.js

And just load the jemplate_source.js and eval it in the JS engine.

Just for note: On my noteboook, using the original TemplateToolkit i got 10k/sec. The above Jemplate/Duktape only 5k/sec.

The my original answer:

Here is Shotenjin what is derived from a Tenjin template system. (the perl Tenjin is here.

Shotenjin is joose based, so with some plus work will be possible use Shotenjin from a perl with Javascript::V8. But it is still not exacly for what youre looking.

EDIT: For what you're looking is already done - unfortunately, for the RUBY. https://github.com/elado/isotope

EDIT2: Just discovered: here is Template::JavaScript what is TT compiled into JS and executed with v8 server side...

Jemplate

(That said, I disagree entirely with your premise of "Javascript is full featured language so using it is probably better/faster than some "mini languages" like TT or similar" - IMO there is absolutely no reason to do what you are asking.)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!