Perl:Javascript :: V8模板-来自perl [英] Perl: Javascript::V8 templates - from the perl

查看:104
本文介绍了Perl:Javascript :: V8模板-来自perl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正在寻找诸如HTML :: Mason(或Mason)之类的模板引擎,因此将源组件编译"为perl代码的是什么,而不是perl-code会将组件"编译"为JavaScript代码,并在使用Javascript运行/执行它们之后:: V8 perl模块.

Looking for template engine like HTML::Mason (or Mason), so what "compiles" source components into perl code, but instead of perl-code will "compile" components into JavaScript code and after run/execute them with Javascript::V8 perl module.

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

Motivation: Looking for solution for safe template language, what can edit users without compromising the server security. JavaScript is full featured language so using it is probably better/faster than some "mini languages" like TT or similar. The best for me would be an extension (rewrite) of Mason for compiling into Joose/JavaScript instead of Moose/Perl. ;)

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

And yes, want do this from perl with Javascript::V8 because this way is possible having all perl's power available via Javascript::V8 $context->bind_function in very safe way.

问题:

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

在梅森,您可以编写例如

in Mason you can write for example

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

很高兴有可能用JS编写上述代码,例如:

would be nice to have possibility write the above in JS, like:

% //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>

应将以上源代码编译"为JavaScript(Joose),并使用Javascript :: V8执行. (就像在Mason中一样-源代码被编译成perl/Moose对象并用perl执行)...

The above source should be "compiled" into JavaScript (Joose), and executed with Javascript::V8. (like in Mason - the source is compiled into perl/Moose object and executed with perl)...

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

As you can see, the for(var i in list) is written in pure JS, and not in "mini-language"…

推荐答案

多年后重新访问和)

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

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).

此外,这也是如何在Duktape引擎中使用 great @ysth的答案中的Jemplate(服务器端)的方法.

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)
    );
!);

产生

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

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

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

然后只需加载jemplate_source.js并将其评估到JS引擎中即可.

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

仅作说明:在我的笔记本上,使用原始的 TemplateToolkit ,我得到了10k/sec.上面的Jemplate/Duktape仅5k/sec.

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

我的原始答案:

这里是 Shotenjin 源自Tenjin模板系统. (perl Tenjin在这里.

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

Shotenjin是基于joose的,因此可以通过使用Javascript :: V8从perl中使用Shotenjin进行一些额外的工作.但是,对于您的外观来说仍然不完美.

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.

对于您想要的东西,已经完成了-不幸的是,对于RUBY. https://github.com/elado/isotope

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

刚刚发现:这是 Template :: JavaScript TT编译成的内容JS,并通过v8服务器端执行...

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

这篇关于Perl:Javascript :: V8模板-来自perl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆