Dancer--introduction小议?
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. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |