莫名的路线不起作用

时间:2013-11-14 08:29:15

标签: perl mojolicious

我正在查看文档 http://mojolicio.us/perldoc/Mojolicious/Lite

我正在粘贴教程中的示例,我几乎总是收到此错误消息:

找不到页面......

我试过的最后一个例子是:

use Mojolicious::Lite;

  get '/with_layout';

  app->start;
  __DATA__

  @@ with_layout.html.ep
  % title 'Green';
  % layout 'green';
  Hello World!

  @@ layouts/green.html.ep
  <!DOCTYPE html>
  <html>
    <head><title><%= title %></title></head>
    <body><%= content %></body>
  </html>

这是我得到的错误

[Thu Nov 14 03:43:15 2013] [debug] GET "/with_layout".
[Thu Nov 14 03:43:15 2013] [debug] Template "with_layout.html.ep" not found.
[Thu Nov 14 03:43:15 2013] [debug] Template "not_found.development.html.ep" not found.
[Thu Nov 14 03:43:15 2013] [debug] Template "not_found.html.ep" not found.
[Thu Nov 14 03:43:15 2013] [debug] Rendering cached inline template.
[Thu Nov 14 03:43:15 2013] [debug] Rendering cached inline template.
[Thu Nov 14 03:43:15 2013] [debug] 404 Not Found (0.011649s, 85.844/s).

1 个答案:

答案 0 :(得分:5)

你错过了定义路线的子程序吗?!

来自docs你可以得到这个:

use Mojolicious::Lite;

# Route leading to an action that renders a template
get '/with_layout' => sub {
    my $self = shift;
    $self->render('foo');
};

app->start;
__DATA__

@@ with_layout.html.ep
<!DOCTYPE html>
<html>
  <head><title><%= title %></title></head>
  <body><%= content %></body>
</html>

更好的是:

get '/with_layout' => sub {
    my $self = shift;
    $self->render(template => 'with_layout', format => 'html', handler => 'ep');
    return;
};

现在错误:

Template "not_found.development.html.ep" not found.
Template "not_found.html.ep" not found.

您可以在模板目录中创建这些文件

/lite_app.pl
/templates/not_found.development.html.ep
/templates/not_found.html.ep