Perl: Javascript :: V8 Templates - from perl

You are looking for a template engine, such as HTML :: Mason (or Mason), so that it “compiles” the source components into perl code, but instead of perl code they will “compile” the components into JavaScript code and after running / executing them using Javascript: : V8 module perl.

Motivation: finding a solution for a secure template language that can edit users without compromising server security . JavaScript is a full-featured language, so its use is probably better / faster than some “mini-languages" such as TT or similar. Best for me would be a Mason extension (rewriting) to compile in Joose / JavaScript instead of Moose / Perl .;)

And yes, you want to do this with perl using Javascript :: V8, because this method is possible by having the very available power of perl through Javascript :: V8 $ context-> bind_function.

Questions:

  • Does anyone know something like? (nothing found in CPAN) ...

EDIT: 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> 

it would be nice to be able to write above in JS, for example:

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

The above source must be "compiled" in JavaScript (Joose) and executed using Javascript :: V8. (as in Mason - the source is compiled into a perl / Moose object and executed with perl) ...

As you can see, for(var i in list) written in pure JS, not in a "mini-language" ...

+8
perl v8 mason
source share
2 answers

Re-editing and editing over the years :)

Here is the EJS :: Template . It does exactly what you requested - it compiles the templates in JS and uses the V8 (or even JE ) mechanisms to evaluate. Unfortunately, Javascript :: Duktape engine support is not supported.

In addition, there is a snipet here on how to use the Jemplate (server side) from the big @ysth 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 pre-compiling the templates using the Jemplate , for example:

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

And just download jemplate_source.js and evaluate it in the JS engine.

Note only: on my note, using the original TemplateToolkit , I got 10 fps. The aforementioned Jemplate / Duktape is only 5 fps.

My original answer:

Here is the Shotenjin that comes from the Tenjin template system. (perl Tenjin is here .

Shotenjin is box-based, so with some extra work you can use Shotenjin from perl with Javascript :: V8. But that is not how you look.

EDIT: Already done for you - unfortunately, for RUBY. https://github.com/elado/isotope

EDIT2: just discovered: here is Template :: JavaScript that TT is compiled in JS and executed on the v8 server side ...

+9
source share

Jemplate

(However, I completely disagree with your premise "Javascript is a fully functional language, so its use is probably better / faster than some" mini-languages ​​"such as TT or the like" - IMO there is absolutely no reason to do that what are you asking.)

+4
source share

All Articles