til

Today I Learned: collection of notes, tips and tricks and stuff I learn from day to day working with computers and technology as a developer

View project on GitHub

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 mediatypes 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