How do I call a script from a template?
In a template I invoke (successfully) from a Dancer2 path I have some jquery that works outside Dancer2: $.post('/data1.pl',data,function(returnData){ $('#func').html(returnData) This code calls data1.pl and stuff its output into the div func. I know data1.pl works in a non-Dancer2 environment. But I cannot figure out where to put data1.pl so it can be run from the template. I tried just putting it in the dancer public (where the css folder, dispatch.cgi and such are), but that didn't work. I tried even to run it from the browser command line and the browser wants to download it. The directory has +ExecCGI and Dancer2 runs. Where should I put this file and what must I do so I can run it? Thanks! --john -- John J. McDermott, CPLP Learning and Performance Consultant jjm at jkintl.com 575/737-8556 Check out my security blog posts <http://cybersecurity.learningtree.com> Add an A for the Arts To STEM and get STEAM and a strong engine to move forward.
On Wed, 2015-04-29 at 16:12 -0600, John J. McDermott, CPLP wrote:
In a template I invoke (successfully) from a Dancer2 path I have some jquery that works outside Dancer2: $.post('/data1.pl',data,function(returnData){ $('#func').html(returnData)
This code calls data1.pl and stuff its output into the div func.
I know data1.pl works in a non-Dancer2 environment. But I cannot figure out where to put data1.pl so it can be run from the template.
Surely the "correct" way of doing this is to rewrite data1.pl into your main Dancer app, as its own route? Is there any reason that you cannot do that?
I tried just putting it in the dancer public (where the css folder, dispatch.cgi and such are), but that didn't work.
I would keep it out of there, as it's nothing to do with Dancer.
I tried even to run it from the browser command line and the browser wants to download it. The directory has +ExecCGI and Dancer2 runs.
If you have to keep it separate to Dancer, then that's the way to do it. Put it in a completely separate area of your web server, and ensure it can be executed as its own independent script. If the browser is trying to download it, then something is wrong with your webserver configuration. I'm sure people will be happy to try and assist, but that is not a Dancer problem, it is a generic web/perl problem. Andy
On 4/30/2015 12:49 PM, Andrew Beverley wrote:
On Wed, 2015-04-29 at 16:12 -0600, John J. McDermott, CPLP wrote:
In a template I invoke (successfully) from a Dancer2 path I have some jquery that works outside Dancer2: $.post('/data1.pl',data,function(returnData){ $('#func').html(returnData)
This code calls data1.pl and stuff its output into the div func.
I know data1.pl works in a non-Dancer2 environment. But I cannot figure out where to put data1.pl so it can be run from the template. Surely the "correct" way of doing this is to rewrite data1.pl into your main Dancer app, as its own route? Is there any reason that you cannot do that? Well I have a page accessed through a template. Let's call the template x.tt. Now I want to somehow change the content of a div on that page. I know how to do that. But the code is perl. If I redirect through another route, it will reload the page or potentially have other issues as it only returns a brief string (i.e) not JSON, or am I incorrect?
I tried just putting it in the dancer public (where the css folder, dispatch.cgi and such are), but that didn't work. I would keep it out of there, as it's nothing to do with Dancer.
I tried even to run it from the browser command line and the browser wants to download it. The directory has +ExecCGI and Dancer2 runs. If you have to keep it separate to Dancer, then that's the way to do it. Put it in a completely separate area of your web server, and ensure it can be executed as its own independent script.
If the browser is trying to download it, then something is wrong with your webserver configuration. I'm sure people will be happy to try and assist, but that is not a Dancer problem, it is a generic web/perl problem. Hmmm, the Dancer part is "where to executables go that can be called via a browser viewing a Dancer-generated template?
Thanks! --john
Andy
_______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
-- John J. McDermott, CPLP Learning and Performance Consultant jjm at jkintl.com 575/737-8556 Check out my security blog posts <http://cybersecurity.learningtree.com> Add an A for the Arts To STEM and get STEAM and a strong engine to move forward.
On Thu, 2015-04-30 at 13:32 -0600, John J. McDermott, CPLP wrote:
I know data1.pl works in a non-Dancer2 environment. But I cannot figure out where to put data1.pl so it can be run from the template.
Surely the "correct" way of doing this is to rewrite data1.pl into your main Dancer app, as its own route? Is there any reason that you cannot do that?
Well I have a page accessed through a template. Let's call the template x.tt. Now I want to somehow change the content of a div on that page. I know how to do that. But the code is perl. If I redirect through another route, it will reload the page
No, it won't reload the page: Dancer will see it as just another HTTP request, and will treat it independently to the first; the web browser knows that it is loading a small chunk of content, as it would if it was your original data1.pl script.
or potentially have other issues as it only returns a brief string (i.e) not JSON, or am I incorrect?
You can make the Dancer route return whatever you want, be it JSON, HTML or anything else. Normally one uses the "template" keyword to return a full HTML page, but you could return anything else and can also set the various headers accordingly. You might like to have a look at Dancer(2)::Plugin::Ajax, which should make some of that a bit simpler.
I tried just putting it in the dancer public (where the css folder, dispatch.cgi and such are), but that didn't work. I would keep it out of there, as it's nothing to do with Dancer.
I tried even to run it from the browser command line and the browser wants to download it. The directory has +ExecCGI and Dancer2 runs. If you have to keep it separate to Dancer, then that's the way to do it. Put it in a completely separate area of your web server, and ensure it can be executed as its own independent script.
If the browser is trying to download it, then something is wrong with your webserver configuration. I'm sure people will be happy to try and assist, but that is not a Dancer problem, it is a generic web/perl problem.
Hmmm, the Dancer part is "where to executables go that can be called via a browser viewing a Dancer-generated template?
Of course, it's loosely related to Dancer. I just mean that the configuration and generation of that on the server-side is completely separate to Dancer, and therefore no different as if you weren't using Dancer. Andy
I had not seen that plugin for some reason. Thanks, Andy. Dancer is far more versatile that I had thought. --john On 5/1/2015 1:25 AM, Andrew Beverley wrote:
You can make the Dancer route return whatever you want, be it JSON, HTML or anything else. Normally one uses the "template" keyword to return a full HTML page, but you could return anything else and can also set the various headers accordingly. You might like to have a look at Dancer(2)::Plugin::Ajax, which should make some of that a bit simpler.
-- John J. McDermott, CPLP Learning and Performance Consultant jjm at jkintl.com 575/737-8556 Check out my security blog posts <http://cybersecurity.learningtree.com> Add an A for the Arts To STEM and get STEAM and a strong engine to move forward.
Hi! 2015-04-30 1:12 GMT+03:00 John J. McDermott, CPLP <jjm@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. -- Wbr, Kõike hääd, Gunnar
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 += "¶m2="+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@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.
Thanks. I get it now. For some reason my mind was clearly clouded with only using Dancer the way I had been. On 5/1/2015 3:55 AM, Hugues wrote:
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 += "¶m2="+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@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.
_______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
-- John J. McDermott, CPLP Learning and Performance Consultant jjm at jkintl.com 575/737-8556 Check out my security blog posts <http://cybersecurity.learningtree.com> Add an A for the Arts To STEM and get STEAM and a strong engine to move forward.
participants (4)
-
Andrew Beverley -
Hugues -
John J. McDermott, CPLP -
WK