【问题标题】:How to use common template for mojolocious lite app?如何为 mojolicious lite 应用程序使用通用模板?
【发布时间】:2012-10-22 08:13:58
【问题描述】:
 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中

【问题讨论】:

    标签: perl mojolicious mojolicious-lite


    【解决方案1】:

    使用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>
    

    【讨论】:

      猜你喜欢
      • 2015-10-06
      • 2017-07-04
      • 2014-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-03
      相关资源
      最近更新 更多