Perl:Javascript :: V8模板 - 来自perl

kob*_*ame 8 perl v8 mason joose embedded-v8

寻找像HTML :: Mason(或Mason)这样的模板引擎,所以将源组件"编译"成perl代码,而不是perl-code将"编译"组件到JavaScript代码中并在用Javascript :: V8运行/执行它们之后perl模块.

动机:寻找安全模板语言的解决方案,可以在不影响服务器安全性的情况下编辑用户.JavaScript是全功能语言,因此使用它可能比TT或类似的某些"迷你语言"更好/更快.对我来说最好的是扩展(重写)Mason编译成Joose/JavaScript而不是Moose/Perl.;)

是的,想要使用Javascript :: V8从perl执行此操作,因为这种方式可以通过Javascript :: V8 $ context-> bind_function以非常安全的方式提供所有perl的功能.

问题:

  • 有人知道吗?(在CPAN中什么也没找到)......

编辑:在梅森你可以写例如

% #perl version
% my(@list) = qw(Jane John Doe);
<ul> 
% foreach my $item (@list) { 
  <li><% uc($item) %></li> 
% } 
</ul>
Run Code Online (Sandbox Code Playgroud)

很高兴有可能在JS中编写上述内容,例如:

% //javascript version
% var list = ["Jane", "John", "Doe"];
<ul> 
% for(var i in list) {
  <li><% perl_uc($list[i]) %></li>
  <!-- the "perl_uc" is the real perl uc() what is binded
       with Javascript::V8::bind_function(perl_uc => sub { return uc(@_) }
  -->
% } 
</ul>
Run Code Online (Sandbox Code Playgroud)

上面的源应该"编译"成JavaScript(Joose),并用Javascript :: V8执行.(就像在Mason中一样 - 源被编译成perl/Moose对象并用perl执行)...

正如您所看到的,它for(var i in list)是用纯JS编写的,而不是"迷你语言"......

jm6*_*666 9

多年后重新访问和编辑:)

这是EJS :: Template.它完全符合您的要求 - 将模板编译为JS并使用V8(甚至JE)引擎进行评估.不幸的是,还没有Javascript :: Duktape引擎支持.

另外,这里有一个snipet如何使用Jemplate来自伟大的 @ 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)
    );
!);
Run Code Online (Sandbox Code Playgroud)

产生

pope: vroom = 1007479
pope: BrowserUk = 167247
pope: Corion = 133975
pope: ikegami = 128977 
Run Code Online (Sandbox Code Playgroud)

您可以通过使用jemplate命令预先编译模板来省略所有Jemplate调用,例如:

jemplate --runtime=lite --compile /path/to/templates > jemplate_source.js
Run Code Online (Sandbox Code Playgroud)

只需jemplate_source.js在JS引擎中加载并评估它.

只是为了注意:在我的noteboook上,使用原始的TemplateToolkit我得到了10k/sec.上面的Jemplate/Duktape只有5k/sec.

我原来的回答是:

这是Shotenjin什么是从天神模板系统派生的.(perl 天神在这里.

Shotenjin是基于joose的,所以有一些加工可以使用Javascript :: V8的perl使用Shotenjin.但对于你正在寻找的东西,它仍然不是很明显.

编辑:对于你所看到的已经完成 - 不幸的是,对于RUBY.https://github.com/elado/isotope

EDIT2:刚刚发现:这里是Template :: JavaScript什么是TT编译成JS并用v8服务器端执行...