in reply to Why won't my Dancer2 app render any output???

Hi,

One thing to note is that only the last engines block will have effect in your config (although that is clearly not the source of this problem).

I was able to reproduce your issue with the test app generated by dancer2 gen -a by adding a line after the template ... statement. My failing file:

package My::TestApp; use Dancer2; our $VERSION = '0.1'; get '/' => sub { template 'index' => { 'title' => 'My::TestApp' }; debug 'after templating'; }; true;
The debug log statement was printed to the log but no output was shown in the browser. Removing/commenting the line fixed the problem. I believe this is because the output of the last line in the handler is what is returned to the browser. (From the doc: "Note that template simply returns the content, so when you use it in a route handler, if execution of the route handler should stop at that point, make sure you use return to ensure your route handler returns the content.") Adding a line with output shows this:
package My::TestApp; use Dancer2; our $VERSION = '0.1'; get '/' => sub { template 'index' => { 'title' => 'My::TestApp' }; debug 'after templating'; 'foo'; }; true;
The browser shows 'foo' with this code.

Do you have any other code in the route handler in sandbox.pm after the template statement?

Hope this helps!

Update: added quote from the doc


The way forward always starts with a minimal test.

Replies are listed 'Best First'.
Re^2: Why won't my Dancer2 app render any output???
by traveler (Parson) on Apr 06, 2025 at 21:06 UTC
    Thanks. I took out both debugs. Now I have:
    get '/login' => sub { # Display a login page; the original URL they requested is availab +le as # param('requested_path'), so could be put in a hidden field in th +e form # debug "displaying login page"; template 'login', { path => param('requested_path') }; };
    and still no output. I tried the "foo" and didn't see that either. I am getting the error:
    [sandbox:22720] core @2025-04-06 16:28:17> Failed to serialize content +: hash- or arrayref expected (not a simple scalar, use allow_nonref t +o allow this) at /usr/local/share/perl/5.34.0/Dancer2/Serializer/JSON +.pm line 40. in (eval 333) l. 1
    But I can't tell what's causing it. When I put the debug before the "template" call, the error vanished. Curious. Also, I have seen examples with multiple engines in the config. Can you point me to an example that says only one is allowed? I had 2 in the old working version. Updates: I found that beginning with 1.3091 it is again permitted to have statement after template. I did a debug template ... and the proper "output" is in the log. Now I need to figure out where it's going...

      I was able to produce that JSON error by adding set serializer => 'JSON'; to the app package, and then requesting the URI with the Accept header set to 'application/json'.

      Could that be the cause of your error?


      The way forward always starts with a minimal test.
        YES!

        Thank you! Thank you! Thank you! The site displays and functions correctly, now.

        I never would have found it otherwise!

      With regard to the engines question, from the config file created in the generated app:

      # NOTE: All the engine configurations need to be under a single "engines:"
      # key.  If you uncomment engine configurations below, make sure to delete
      # all "engines:" lines except the first.  Otherwise, only the last
      # "engines:" block will take effect.
      


      The way forward always starts with a minimal test.
        Thanks. Mine didn't have that comment.