Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Dancer not displaying Template

by nathaniels (Acolyte)
on Sep 21, 2013 at 05:45 UTC ( #1055106=perlquestion: print w/ replies, xml ) Need Help??
nathaniels has asked for the wisdom of the Perl Monks concerning the following question:

I've recently been toying with moving some Perl code into a web setting using Dancer. Below is some code to generate a list of files in a specific folder and then take in the user's selection. However, when run through the browser it simply generates a blank page. What's confusing for me is that the unrelated code following the template determines whether or not it will generate the page.

This works:

any ['get', 'post'] => '/parsescript' => sub { template 'test.tt'; };

This doesn't:

any ['get', 'post'] => '/parsescript' => sub { template 'test.tt'; my @filelist; my $rawtext; my $rootdir = 'C:\\Users\\Nathaniel\\Corpus\\'; opendir (DIR, $rootdir) or die $!; while (my $file = readdir(DIR) ) { next unless (-f "$rootdir/$file"); next unless ($file =~ /\.txt$/); push @filelist, $file ; } };

Thanks for your help!

UPDATE:Thank you to everyone for their help. As always, one of the best results of posting on here is learning about all the other small mistakes or inefficiencies in your code unrelated to the code-breaking problem. Special thanks to CountZero, it would probably have taken me months to realize template must go at the end of the sub. Turns out my biggest mistake (which I omitted in my post as it seemed irrelevant) was trying to pass a list (rather than a reference to a list) to the template. Thank you all!

Comment on Dancer not displaying Template
Select or Download Code
Re: Dancer not displaying Template
by Corion (Pope) on Sep 21, 2013 at 07:53 UTC

    I found Dancer to swallow error messages and die messages, and also to swallow output when warnings are issued during the runtime.

    The workaround I've used in anger is to wrap my code in my own eval and the output the caught information myself. Which defeats the point of using a framework.

    In your case, I would look closely at whether the code could die (maybe $rootdir doesn't exist, etc.) or whether it could output warnings (which doesn't seem to be likely).

    Update: See CountZeros reply, which is the more propable cause I guess - your handler needs to return the proper content through template, so template should be mostly called as

    return template ...;
Re: Dancer not displaying Template
by choroba (Abbot) on Sep 21, 2013 at 08:27 UTC
    You should call template as the last command in a sub. Also, you are not giving any parameters to the template - you probably need to pass @filelist to it.
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Dancer not displaying Template
by CountZero (Bishop) on Sep 21, 2013 at 18:43 UTC
    Nothing to do with your problem, but you do not need to double-up the backslashes and even don't need to use back-slashes in the filepath.
    my $rootdir = 'C:/Users/Nathaniel/Corpus/';
    works equally well and is less typing.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics
Re: Dancer not displaying Template
by CountZero (Bishop) on Sep 21, 2013 at 18:57 UTC
    From the Dancer docs about the template function:
    Returns the response of processing the given template with the given parameters
    In other words, whatever the sub returns goes to the client's browser, which is not necessarily what was returned by template. Hence in your first example you indeed see a webpage and in the second example you "see" the value of the while loop once it ends. As that is unlikely to be a properly formed web-page there is nothing to see. So put the call to template at the very end of the sub.

    And don't forget to pass @filelist to the template function (read the docs on how to do this).

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2014-08-29 08:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (277 votes), past polls