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

Andrew Beverley andy at andybev.com
Fri May 1 08:25:04 BST 2015


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





More information about the dancer-users mailing list