Dancer--introduction小议❸

Dancer--introduction小议❸,第1张

概述Then in App/User/Routes.pm: 导入了上面的模块 use Dancer ':syntax';  get '/user/view/:id' => sub {     ... };  LOGGING----->日志 It's possible to log messages sent by the application. In the current version, onl

Then in App/User/Routes.pm:

导入了上面的模块

use Dancer ':Syntax'; 

get '/user/vIEw/:ID' => sub {    

...

}; 

LOGGING----->日志

It's possible to log messages sent by the application. In the current version, only one method is possible for logging messages but future releases may add additional logging methods, for instance logging to syslog.

我们可以记录应用发来的日志信息。在现在的dancer版本中,对于日志的记录只能使用一个方法,但是在未来的版本中也许会加入其它的日志记录方法,比如将日志记录到syslog服务中,也就是说,日志记录功能与syslog服务相结合。

In order to enable the logging system for your application, you first have to start the logger engine in your config.yml

如果想对一个应用启用日志系统,那么你首先需要在配置文件config.yml中开启日志记录器引擎。

logger: 'file'

你还可以改变日志的记录级别

Then you can choose which kind of messages you want to actually log:

log: 'deBUG'     # will log deBUG, warning, error and info messages

log: 'info'      # will log info, warning and error messages

log: 'warning'   # will log warning and error messages

log: 'error'     # will log error messages 

日志级别如下:

log level: deBUG info warning error

A directory appdir/logs will be created and will host one logfile per environment. The log message contains the time it was written, the PID of the current process, the message and the caller information (file and line).

日志目录appdir/logs会为每个环境保存一个日志文件,日志信息包含日志的写入时间,当前进程的PID,具体的信息(应用返回来的)和caller函数信息(包含文件和行号)

To log messages, use the deBUG, info, warning and error functions. For instance:

如果想要记录信息除了需要设置上面的config.yml配置文件外,还需要使用deBUG、info、warning和error函数。比如:

deBUG 'This is a deBUG message'; 

USING TEMPLATES------>使用模板

VIEWS------->视图

It's possible to render the action's content with a template; this is called a vIEw. The 'appdir/vIEws' directory is the place where vIEws are located.

我们可以使用一个模板来发送动作的内容,这种方法就称作"视图"。视图被放置在"/appdir/vIEws"目录下。

You can change this location by changing the setting 'vIEws', for instance if your templates are located in the 'templates' directory, do the following:

你也可以通过设置vIEws来改变视图的存放地,例如,如果你想将视图存放在"templates"目录下,那么你需要做如下设置:

set vIEws => path(dirname(__file__), 'templates'); 

By default, the internal template engine is used (Dancer::Template::Simple) but you may want to upgrade to Template::Toolkit. If you do so, you have to enable this engine in your settings as explained in Dancer::Template::Templatetoolkit. If you do so, you'll also have to import the Template module in your application code. Note that Dancer configures the Template::Toolkit engine to use <% %> brackets instead of its default [% %] brackets, although you can change this in your config file.

默认情况下,使用内部的(Dancer::Template::Simple)模板引擎,但是你也可以将模板引擎更新为Template::Toolkit。如果想这样做,那么你必须你配置中开启Dancer::Template::Templatetoolkit引擎功能。如果想开启该引擎,你需要在应用代码中导入Template模块。注意,Dancer配置Template::Toolkit引擎时,使用"<% %> "样式的括弧,而不是"[% %]"样式的括弧,当然,你也可以在配置文件中来修改dancer的模板引擎模式。

All vIEws must have a '.tt' extension. This may change in the future.

现在版本中,所有的视图都必须以".tt"作为扩展名,但是在未来的版本中也许会改变这种形式。

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):

如果想使用一个视图,你需要的做的事很简单,仅仅需要在动作的末尾调用关键字"template"然后分配一个视图名字和一个标志hashref(hash引用)即可(注意:在视图中会自动的以request、session和params的形式来访问请求、会话和路由参数。)。

use Dancer;

use Template; 

get '/hello/:name' => sub {

    template 'hello' => { 

number => 42 

};

}; 

And the appdir/vIEws/hello.tt vIEw can contain the following code:

下面是/vIEws/hello.tt视图包含的代码

<HTML> 

 <head></head> 

 <body>

  <h1>Hello <% params.name %></h1>     

  <p>Your lucky number is <% number %></p>     

  <p>You are using <% request.user_agent %></p>     

  <% IF session.user %>         

<p>You're logged in as <% session.user %></p>     

  <% END %> 

 </body>

</HTML> 

LAYOUTS------>布局

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.

一个布局是一个特殊的视图,布局存放在"layouts"目录下(layout目录存放在vIEws目录下),布局必须有一个'content'标志性的名字,该标志性的名字标记了将使用动作视图的地方。这样你可以定义一个全局的布局。当你调用"template"关键字的时候,任何一个已经定义的"标志"在布局中都是可见的,同样还有标准的会话,请求和参数标志。这样你就可以将以页为单位的内容插入到HTML标准模板中。像页面的标题,如同导航(书籍的目录)一样,指定当前页面的标志。(总感觉这个地方的描述像在说明---->该功能就像书籍的目录或者网页中的超级链接,能将你引导到某个你想要去的地方,或者点击一下,能够知道当前在网页哪个地方,具体不懂)。

Here is an example of a layout: vIEws/layouts/main.tt:

下面是一个布局的例子

<HTML>

    <head><% page_Title %></head>

    <body>    

<div ID='header'>

    ...

    </div>

<div ID='content'> 

<% content %>

    </div>

</body>

</HTML> 

This layout can be used like the following:

对于上面的布局下面给出了一个使用的例子:

use Dancer;

set layout => 'main'; 

get '/' => sub {

    template 'index' => { page_Title => 'Your website Homepage' };

}; 

Of course, if a layout is set, it can also be Disabled for a specific action, like the following:

当然,如果一个布局被设置了,她也可以针对某个特定的动作而被禁用,如下:

use Dancer;

set layout => 'main'; 

get '/nolayout' => sub {

    template 'some_AJAX_vIEw',

{ tokens_var => '42' },

{ layout => 0 };

}; 

STATIC fileS------->静态文件

STATIC DIRECTORY------->静态目录

Static files are served from the ./public directory. You can specify a different location by setting the 'public' option:

静态文件保存在appdir/public目录下,你可以通过"public"选项设置来指定一个不同的目录。

[root@dou public]# pwd

/root/MyWebApp/public

[root@dou public]# ls

404.HTML  CSS(dir)           dispatch.fcgi  images(dir)

500.HTML  dispatch.cgi  favicon.ico    JavaScripts(dir)

改变静态文件存放地:

set public => path(dirname(__file__), 'static'); 

注意:public目录名不包含在URL中,如果要想访问静态文件需要如下表示:比如要访问静态文件/public/CSS/style.CSS 需要使用example.com(主机名)/CSS/style.CSS.

Note that the public directory name is not included in the URL. A file ./public/CSS/style.CSS is made available as example.com/CSS/style.CSS.

总结

以上是内存溢出为你收集整理的Dancer--introduction小议❸全部内容,希望文章能够帮你解决Dancer--introduction小议❸所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1283447.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-09
下一篇 2022-06-09

发表评论

登录后才能评论

评论列表(0条)

保存