多年后重新审视和编辑:)
这里是EJS::模板 https://metacpan.org/pod/EJS::Template。
它完全符合您的要求 - 将模板编译为 JS 并使用V8
(甚至JE
)用于评估的引擎。不幸的是,没有Javascript::Duktape https://metacpan.org/pod/JavaScript::Duktape引擎支持(尚未)。
另外,这里有一个如何使用的片段Jemplate
(服务器端)从great@ysth 使用 Duktape 引擎的答案。
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
您可以通过使用以下命令预先编译模板来省略所有 Jemplate 调用jemplate
命令,例如:
jemplate --runtime=lite --compile /path/to/templates > jemplate_source.js
只需加载jemplate_source.js
并在 JS 引擎中对其进行评估。
请注意:在我的笔记本上,使用原始的模板工具包 https://metacpan.org/pod/Template我有 10k/秒。上面的 Jemplate/Duktape 只有 5k/秒。
我原来的答案:
Here is 书天神 https://github.com/SamuraiJack/Shotenjin源自 Tenjin 模板系统的内容。 (perl天神来了 http://search.cpan.org/~idoperel/Tenjin-0.070001/lib/Tenjin.pm.
Shotenjin 是基于 joose 的,因此通过一些额外的工作将可以使用来自 perl 的 Shotenjin 和 Javascript::V8。但它仍然不完全符合您的需求。
编辑:对于您正在寻找的内容已经完成 - 不幸的是,对于红宝石。https://github.com/elado/isotope https://github.com/elado/isotope
EDIT2:刚刚发现:这里是模板::JavaScript https://metacpan.org/module/Template::JavaScript什么是TT编译成JS并在v8服务器端执行...