Skip to content

Latest commit

 

History

History
38 lines (29 loc) · 1015 Bytes

respond_to_and_template_parameter.md

File metadata and controls

38 lines (29 loc) · 1015 Bytes

Mojolicious::Controller respond_to and template parameter

Mojolicious is incredibly helpful and invites to fast development. Please note that the respond_to method for the Mojolicious::Controller class can help you to support several media types easily.

Example lifted from the documentation:

$c = $c->respond_to(
  json => {json => {message => 'Welcome!'}},
  html => {template => 'welcome'},
  any  => sub {...}
);

The template parameter for HTML however does have to have the complete path, reflecting your structure in your templates/ directory.

> tree templates/
templates/
├── example
│   └── welcome.html.ep
└── layouts
    └── default.html.ep

Initial example modified accordingly:

$c = $c->respond_to(
  json => {json => {message => 'Welcome!'}},
  html => {template => 'example/welcome'},
  any  => sub {...}
);

References