[dancer-users] How do I call a script from a template?

Hugues hugues at max4mail.com
Fri May 1 10:55:15 BST 2015


Hello
from you html code you can call a route, and this route can call a perl 
script ,  not directly a perl script
see this example

My route:

use Dancer::Plugin::Ajax
ajax '/example/add' => sub {

          my $parm1       = params->{'param1'  } // undef;
          my $param2     = params->{'param2'   } // undef;
          my $param3     = params->{'param3' } // undef;
....


                                   my $cmd = "/usr/bin/sudo 
/home/example.pl &";
                                   my $result = `$cmd`;
                                   warning $result;
$result=decode('UTF8',$result);

                                   if ( $result ) {

                                           return template 
'prelevement/list' => {
show_warning    => "Erreur<h4>$result</h4>",
                                           };

                                   } else {

                                          return template 
'prelevement/list' => {
show_success    => "Calcul <h4>please wait </h4>",
                                          };
                                   }

};






My js jquery code in my template ( use class="Example" )


$(".Example").click(function(e){
            var param1       = $(this).attr('param1');
            var param2       = $(this).attr('param2');
            var param3        = $(this).attr('param3');
            var DATA           = "param1="+param1.toString();
                DATA          += "&param2="+param2.toString();
.....

.....



           // fonction Ajax.
           if ( Statut != 'Ok' ) {
                         $.ajax({
                                 url: "/example/add",
                                 dataType: "JSON",
                                 data: DATA,
                                 success:function(data){
                                         // Ne changer la couleur du 
bouton que pour info
                                         if (! BoutonCouleur ) {
$('#'+Name).removeClass('btn-default').addClass('btn-success');
$('#'+Name).html('Ok');
                                         }
                                 },
                                 error: function(data) {
                                 $('#'+Name).html('Erreur');
$('#'+Name).removeClass('btn-default').addClass('btn-danger');
                                 }


                         });
           }
});


Le 01/05/2015 10:56, WK a écrit :
> Hi!
>
> 2015-04-30 1:12 GMT+03:00 John J. McDermott, CPLP <jjm at jkintl.com>:
>
>> In a template I invoke (successfully) from a Dancer2 path I have some jquery
>> that works outside Dancer2:
> Like Andrew pointed, you don't call anything from template, you use
> template to generate HTML-page, which contains javascript and
> client/browser makes separate request to get some content from server.
> This content could come from other route or from other server,
> whatever. So, instead of script data1.pl seems reasonable to have a
> route in your Dancer app, which answers to those requests.
>
> Client side (javascript in browser) does not know anything of your
> serverside content generation, so templates do not matter here.
>



More information about the dancer-users mailing list