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. Everything before the last slash will be interpreted as the subdirectory path in which to find the template.

The best possible representation will be automatically selected from the Accept request header, format stash value or format GET/POST parameter and stored in the format stash value. To change MIME type mappings for the Accept request header or the Content-Type response header you can use "types" in Mojolicious.

You can also change the templates of those pages, since you most likely want to show your users something more closely related to your application in production. The renderer will always try to find exception.$mode.$format.* or not_found.$mode.$format.* before falling back to the built-in default templates.

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.

This chain could go on and on to allow a very high level of template reuse.

Form validation

You can use "validation" in Mojolicious::Controller to validate GET/POST parameters submitted to your application. All unknown fields will be ignored by default, so you have to decide which should be required or optional before you can perform checks on their values. Every check is performed right away, so you can use the results immediately to build more advanced validation logic with methods like "is_valid" in Mojolicious::Validator::Validation.

Form elements generated with tag helpers from Mojolicious::Plugin::TagHelpers will automatically remember their previous values and add the class field-with-error for fields that failed validation to make styling with CSS easier.

Chunked transfer encoding

For very dynamic content you might not know the response content length in advance, that's where the chunked transfer encoding and "write_chunk" in Mojolicious::Controller come 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.

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 command Mojolicious::Command::inflate will write all templates and static files from the DATA section into actual files in the templates and public directories.

Adding your favorite template system

Maybe you would prefer a different template system than ep, and there is not already a plugin on CPAN for your favorite one, all you have to do is add a new handler with "add_handler" in Mojolicious::Renderer when register is called.