-1

I am following the Mojolicious lite tutorial but struggling to generate a page with the <pre>content</pre> formatting.

Waht do I need to change so that the output is formatted as a block of <pre>content</pre> HTML?

Here is what I have:

#!/usr/bin/env perl                                                                                      
use Mojolicious::Lite -signatures;

get '/foo' => sub ($c) {
  my $id = $c->param("id");
  my $cmd = "perl /home/user/tool.pl -id $id 2>&1";
  my $ret = `$cmd`;
  # $c->render(text => $id);                                                                             
  $c->render(text => $ret, layout => 'default');
};

app->start;
__DATA__
@@ layouts/foo.html.ep
<!DOCTYPE html>
<html>
  <head>
    <title><%= title %></title>
  </head>
  <body>
  <pre>
    %= content
  </pre>
  </body>
</html>


719016
  • 9,922
  • 20
  • 85
  • 158

1 Answers1

1

The text renderer does not use layouts. You have to use an ep template with a layout file and use the ep renderer. (Which is the default if you call render() without args).

Also note that stash() is used to pass data to the template.

use Mojolicious::Lite -signatures;

get '/foo' => sub ($c) {
  my $id = $c->param("id");

  my $ret = 'test';
  # $c->render(text => $id);
  $c->stash(ret => $ret);
  $c->render();
};

app->start;
__DATA__
@@ foo.html.ep
    % layout 'def';
    %= $ret
@@ layouts/def.html.ep
<!DOCTYPE html>
<html>
  <head>
    <title><%= title %></title>
  </head>
  <body>
  <pre>
    %= content
  </pre>
  </body>
</html>
clamp
  • 2,552
  • 1
  • 6
  • 16