Re: [dancer-users] Main Layout Loading
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. If you use the "template" keyword, then the chances are you're displaying a standard web page, in which case it will be helpfully be wrapped in your main template, as that's probably what you would want.
For example, at following code, why main.tt is loaded at login page (get '/login') but not loaded at Home Page (get '/' )?
get '/' => sub { return "Home Page"; };
Here you're returning only the content "home page". If you want the text to be wrapped in the layout, then you will need to put it in a template and use the template keyword. The short of it is that whatever you return from your route is what gets sent to the browser. The layout is just a part of the template functionality which you can use to return web pages. Andy
On Thu, 21 May 2015 12:08:32 +0100 Andrew Beverley <andy@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@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
Dear Andrew, Thanks. Following is keyword for me. *The layout is just a part of the template functionality which you can use to return web pages.* Dear David, Your solution below worked, thanks. 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"); }; I think the other solution is calling a template as follow *get '/' => sub { template 'dummy.tt <http://dummy.tt>', { 'msg' => 'Home Page', };* *dummy.tt <http://dummy.tt>* *<% msg %>* On Thu, May 21, 2015 at 3:27 PM, David Precious <davidp@preshweb.co.uk> wrote:
On Thu, 21 May 2015 12:08:32 +0100 Andrew Beverley <andy@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@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
_______________________________________________ dancer-users mailing list dancer-users@dancer.pm http://lists.preshweb.co.uk/mailman/listinfo/dancer-users
-- *Kadir BeyazlıComputer Engineer* *GSM : +90 535 821 50 00*
participants (3)
-
Andrew Beverley -
David Precious -
Kadir Beyazlı