[dancer-users] Main Layout Loading

David Precious davidp at preshweb.co.uk
Thu May 21 13:27:56 BST 2015

On Thu, 21 May 2015 12:08:32 +0100
Andrew Beverley <andy at andybev.com> wrote:
> On Thu, 2015-05-21 at 13:04 +0300, Kadir Beyazlı wrote:
> > main.tt is loaded if I use a template at my code.
> > What is the reason main.tt is not loaded if I don't use a template?
> The idea is that you can have full control as to what you return to
> the browser. For example, you might want to return only some simple
> plain text, or maybe send a file to the browser, in which case
> wrapping in the main layout would break things.

^ This is indeed the right answer - applying the layout is part of the
template processing - so if you've used template() to generate content
from a template, the layout is applied, but if you've just returned
content, it's assumed that that's the content you want sent to the
client - in other words, whatever the route handler returns is what
goes to the client, whether that's the result of a template() call, or
just some content you generated.

If you want to apply the layout around some generated content without
having to put it in a template file, you can use the template engine's
apply_layout() method to do it, e.g.:

get '/' => sub {
    # get the template engine
    my $template_engine = engine 'template';
    # apply the layout (not the renderer), and return the result
    return $template_engine->apply_layout("Home Page");

David Precious ("bigpresh") <davidp at preshweb.co.uk>
http://www.preshweb.co.uk/     www.preshweb.co.uk/twitter
www.preshweb.co.uk/linkedin    www.preshweb.co.uk/facebook
www.preshweb.co.uk/cpan        www.preshweb.co.uk/github

More information about the dancer-users mailing list