Templates can be automatically detected if enough information is provided by the developer or routes. Template names are expected to follow the name.format.handler scheme, with name defaulting to controller/action or the route name, format defaulting to html and handler to ep.

The renderer can be easily extended to support additional template systems with plugins, but more about that later.

Embedded Perl

Mojolicious includes a minimalistic but very powerful template system out of the box called Embedded Perl or ep for short. It allows the embedding of Perl code right into actual content using a small set of special tags and line start characters.

There are also many helper functions available, but more about that later.

<%= dumper {foo => 'bar'} %>

BASICS

Most commonly used features every Mojolicious developer should know about.

Automatic rendering

The renderer can be manually started by calling the method "render" in Mojolicious::Controller, but that's usually not necessary, because it will get automatically called if nothing has been rendered after the router finished its work. This also means you can have routes pointing only to templates without actual actions.

$self->render;

There is one big difference though, by calling it manually you can make sure that templates use the current controller object, and not the default controller specified with the attribute "controller_class" in Mojolicious.

Rendering templates

The renderer will always try to detect the right template but you can also use the template stash value to render a specific one.

And if no viable representation could be found, the any fallback will be used or an empty 204 response rendered automatically.

Helpers

Helpers are little functions you can use in templates and controller code.

%= dumper [1, 2, 3]
my $serialized = $self->dumper([1, 2, 3]);

The helper "dumper" in Mojolicious::Plugin::DefaultHelpers for example will use Data::Dumper to serialize whatever data structure you pass it, this can be very useful for debugging. We differentiate between default helpers which are more general purpose like dumper and tag helpers, which are template specific and mostly used to generate HTML tags.

Custom exception and not_found templates

While the built-in exception and not_found templates are very useful during development, you most likely want to show your users something more related to your application in production. That's why Mojolicious will always try to render exception.$mode.$format.* or not_found.$mode.$format.* before falling back to the built-in default templates.

Chunked transfer encoding

For very dynamic content you might not know the response Content-Length in advance, that's where the chunkedTransfer-Encoding comes in handy. A common use would be to send the head section of an HTML document to the browser in advance and speed up preloading of referenced images and stylesheets.

Especially in combination with long inactivity timeouts this can be very useful for Comet (long polling). Due to limitations in some web servers this might not work perfectly in all deployment environments.

Encoding

Templates stored in files are expected to be UTF-8 by default, but that can be easily changed.

All templates from the DATA section are bound to the encoding of the Perl script, so don't forget to use the utf8 pragma if necessary.

use Mojolicious::Lite;
use utf8;
get '/heart';
app->start;
__DATA__
@@ heart.html.ep
I ♥ Mojolicious!

Base64 encoded DATA files

Base64 encoded static files such as images can be easily stored in the DATA section of your application, similar to templates.

@@ favicon.ico (base64)
...base64 encoded image...

Inflating DATA templates

Templates stored in files get preferred over files from the DATA section, this allows you to include a default set of templates in your application that the user can later customize. The inflate command will write all templates and static files from the DATA section into actual files in the templates and public directories.

$ ./myapp.pl inflate

Customizing the template syntax

You can easily change the whole template syntax by loading the ep_renderer plugin with a custom configuration.