Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Loop function in Javascript fails when combined with perl array

by jeffa (Bishop)
on Aug 26, 2015 at 22:50 UTC ( #1140131=note: print w/replies, xml ) Need Help??


in reply to Loop function in Javascript fails when combined with perl array

"Now, if there are 100 headers, i would have to assign variables 100 times separately. so its better to make it dynamic. "

I recommend a hybrid solution that dynamically creates static Javascript:

use strict; use warnings; use Mojolicious::Lite; get '/' => sub { my $c = shift; my @array = qw( foo bar baz qux ); $c->stash( vars => \@array ); $c->render( template => 'js', format => 'html' ); }; app->start; __DATA__ @@ js.html.ep <script> <% for ( 0 .. $#$vars) { %> var headname<%= $_ + 1 %> = "<%= $vars->[$_] %>"; <% } %> </script>

Yields:

<script> var headname1 = "foo"; var headname2 = "bar"; var headname3 = "baz"; var headname4 = "qux"; </script>

UPDATE: Better yet would be to shove them all into a Javascript array:

use strict; use warnings; use Mojolicious::Lite; get '/' => sub { my $c = shift; my @array = map "'$_'", qw( foo bar baz qux ); $c->stash( vars => \@array ); { local $" = ', '; $c->render( template => 'js', format => 'html' ); } }; app->start; __DATA__ @@ js.html.ep <script> var headname = <%== "[@$vars]" %>; alert( headname ); </script>

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

Replies are listed 'Best First'.
Re^2: Loop function in Javascript fails when combined with perl array ( Mojo::JSON )
by Anonymous Monk on Aug 26, 2015 at 23:50 UTC
    Hi friend, Mojo::JSON is core mojo and provides an encode_json , so
    #!/usr/bin/perl -- use Mojolicious::Lite; use Mojo::JSON qw/ /; get '/' => sub { my $c = shift; my @array = ( 'hi', [ qw/ ro sham bo /], 'bye', ); $c->stash( vars => \@array ); $c->render( template => 'hello', format => 'html' ); }; app->start; __DATA__ @@ hello.html.ep <script> var headname = <%== Mojo::JSON::encode_json( $vars ) %>; alert( headname[0] ); </script> __END__ no more templates no more books this is how you run it and what you ge +t $ perl mojo-hello-mojo-json.pl get / [Wed Aug 26 16:55:06 2015] [debug] Your secret passphrase needs to be +changed [Wed Aug 26 16:55:06 2015] [debug] GET "/" [Wed Aug 26 16:55:06 2015] [debug] Routing to a callback [Wed Aug 26 16:55:06 2015] [debug] Rendering template "hello.html.ep" +from DATA section [Wed Aug 26 16:55:06 2015] [debug] 200 OK (0.004393s, 227.635/s) <script> var headname = ["hi",["ro","sham","bo"],"bye"]; alert( headname[0] ); </script>

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1140131]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2019-08-19 08:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If you were the first to set foot on the Moon, what would be your epigram?






    Results (138 votes). Check out past polls.

    Notices?