新灵空间

I will go wherever god leads me to!
Dancer::Introduction Notes

Dancer::Introduction 学习记录

shpeacelover posted @ 2014年3月21日 00:09 in Dancer , 614 阅读

这不是自己第一次学习dancer这个Perl Web 框架了, 也不是第一次学 Dancer::Introduction 这个教程了。但是因为学习了太长时间, 忘得差不多了, 所以现在记录下, 以备查询。

1. The route action is the code reference declared. It can access parameters through the `params' keyword, which returns a hashref. This hashref is a merge of the route pattern matches and the request params.   

2. A route pattern can contain one or more tokens (a word prefixed with ':'). Tokens can be optional, for example:

 

get '/hello/:name?' => sub {
    "Hello there " . (param('name') || "whoever you are!");
};

3. A route can contain a wildcard (represented by a '*'). Each wildcard match will be returned in an arrayref, accessible via the `splat' keyword.

 

get '/download/*.*' => sub {
	my ($file, $ext) = splat;
	return "file is ".$file." and ext is ".$ext;
};

4. In order to tell Dancer to consider the route as a real regexp, the route must be defined explicitly with qr{}, like the following:get qr{/regular/([\w]+)} => sub {

	my ($name) = splat;
	return "Hello $name";
};

5. Before hooks are evaluated before each request within the context of the request and can modify the request and response. It's possible to define variables which will be  accessible in the action blocks with the keyword 'var'.

6. after hooks are evaluated after the response has been built by a route handler, and can alter the response itself, just before it's sent to the client.  

7. before_template_render hooks are called whenever a template is going to be processed, they are passed the tokens hash which they can alter.  

8. You can use the load method to include additional routes into your application. load is just a wrapper for require, but you can also specify a list of routes files.

9. Dancer configures the Template::Toolkit engine to use <% %> brackets instead of its default [% %] brackets.

10. In order to render a view, just call the 'template' keyword at the end of the action by giving the view name and the HASHREF of tokens to interpolate in the view (note that the request, session and route params are automatically accessible in the view, named request, session and params):

 

use Dancer;
use Template;
 
get '/hello/:name' => sub {
    template 'hello' => { number => 42 };
};

11. A layout is a special view, located in the 'layouts' directory (inside the views directory) which must have a token named `content'. That token marks the place where to render the action view. This lets you define a global layout for your actions. Any tokens that you defined when you called the 'template' keyword are available in the layouts, as well as the standard session, request, and params tokens. This allows you to insert per-page content into the HTML boilerplate, such as page titles, current-page tags for navigation, etc. 

12. When setting up a serializer, a new behaviour is authorized for any route handler you define: any response that is a reference will be rendered as a serialized string, via the current serializer. 

关于这篇教程就写这么多了。

 

 

 

blog comments powered by Disqus