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

Mojolicious includes a minimalistic but very powerful template system out of the box called Embedded Perl or ep for short. It is based on Mojo::Template and allows the embedding of Perl code right into actual content using a small set of special tags and line start characters. For all templates strict, warnings, utf8 and Perl 5.10 features are automatically enabled.

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.

$c->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.

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 formatGET/POST parameter, format stash value or Accept request header 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.

You can break up bigger templates into smaller, more manageable chunks. These partial templates can also be shared with other templates. Just use the helper "include" in Mojolicious::Plugin::DefaultHelpers to include one template into another.

It's never fun to repeat yourself, that's why you can build reusable template blocks in ep that work very similar to normal Perl functions, with the begin and end keywords. Just be aware that both keywords are part of the surrounding tag and not actual Perl code, so there can only be whitespace after begin and before end.

You should always try to keep your actions small and reuse as much code as possible. Helpers make this very easy, they get passed the current controller object as first argument, and you can use them to do pretty much anything an action could do.

Helpers can also accept template blocks as last argument, this for example, allows very pleasant to use tag helpers and filters. Wrapping the helper result into a Mojo::ByteStream object can prevent accidental double escaping.

Similar to stash values, you can use a prefix like myapp.* to keep helpers from getting exposed in templates as functions, and to organize them into namespaces as your application grows. Every prefix automatically becomes a helper that returns a proxy object containing the current controller object and on which you can call the nested helpers.

The helper "content_for" in Mojolicious::Plugin::DefaultHelpers allows you to pass whole blocks of content from one template to another. This can be very useful when your layout has distinct sections, such as sidebars, where content should be inserted by the template.

To build HTML forms more efficiently you can use tag helpers like "form_for" in Mojolicious::Plugin::TagHelpers, which can automatically select a request method for you if a route name is provided. And since most browsers only allow forms to be submitted with GET and POST, but not request methods like PUT or DELETE, they are spoofed with an _method query parameter.

You can use "validation" in Mojolicious::Controller to validate GET and 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.

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.

The optional drain callback ensures that all previous chunks have been written before processing continues. To end the stream you can call "finish" in Mojolicious::Controller or write an empty chunk of data.

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.

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.