如何使用mojolicious lite应用程序的常用模板?

时间:2012-11-01 17:16:10

标签: perl mojolicious mojolicious-lite

 use Mojolicious::Lite;

  # /with_layout
  get '/a' => sub {  my $self = shift;} ;
  get '/b' => sub { my $self = shift;} ; 
  get '/c' => sub { my $self = shift;};

app->start;
__DATA__

@@ a.html.ep
  <!DOCTYPE html>
  <html>
    <head><title><%= title %></title></head>
    <body><%= content %></body>
 <table border="1">
<tr>
<td>row 1, cell 1</td>
<td>row 1, cell 2</td>
</tr>
<tr>
<td>row 2, cell 1</td>
<td>row 2, cell 2</td>
</tr>
</table> 
  </html>

@@ b.html.ep
  <!DOCTYPE html>
  <html>
    <head><title><%= title %></title></head>
    <body><%= content %></body>
 <table border="1">
<tr>
<td>row 1, cell 1</td>
<td>row 1, cell 2</td>
</tr>
<tr>
<td>row 2, cell 1</td>
<td>row 2, cell 2</td>
</tr>
</table> 
  </html>


@@ c.html.ep
  <!DOCTYPE html>
  <html>
    <head><title><%= title %></title></head>
    <body><%= content %></body>
    <table border="1">
<tr>
<td>row 1, cell 1</td>
<td>row 1, cell 2</td>
</tr>
<tr>
<td>row 2, cell 1</td>
<td>row 2, cell 2</td>
</tr>
</table> 
  </html>

我真的很感动

 <table border="1">
    <tr>
    <td>row 1, cell 1</td>
    <td>row 1, cell 2</td>
    </tr>
    <tr>
    <td>row 2, cell 1</td>
    <td>row 2, cell 2</td>
    </tr>
    </table> 

并使用所有html而不是添加到每个html中

1 个答案:

答案 0 :(得分:3)

使用layout模板功能。

use Mojolicious::Lite;

# /with_layout
get '/a' => sub { my $self = shift;};
get '/b' => sub { my $self = shift;}; 
get '/c' => sub { my $self = shift;};

app->start;
__DATA__

@@ a.html.ep
% title 'A';
% layout 'template';
Content for 'A'

@@ b.html.ep
% title 'B';
% layout 'template';
Content for 'B'

@@ c.html.ep
% title 'C';
% layout 'template';
Content for 'C'

@@ layouts/template.html.ep
<!DOCTYPE html>
<html>
  <head><title><%= title %></title></head>
  <body><%= content %></body>
  <table border="1">
  <tr>
    <td>row 1, cell 1</td>
    <td>row 1, cell 2</td>
  </tr>
  <tr>
    <td>row 2, cell 1</td>
    <td>row 2, cell 2</td>
  </tr>
</table> 
</html>