The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

MojoX::Renderer - Renderer

SYNOPSIS

    use MojoX::Renderer;

    my $renderer = MojoX::Renderer->new;

DESCRIPTION

MojoX::Renderer is a MIME type based renderer.

ATTRIBUTES

default_format

    my $format = $renderer->default_format;
    $renderer  = $renderer->default_format('phtml');

Returns the file extesion of the default handler unsed for rendering if called without arguments. Returns the invocant if called with arguments.

handler

    my $handler = $renderer->handler;
    $renderer   = $renderer->handler({phtml => sub { ... }});

Returns a hashref of handlers if called without arguments. Returns the invocant if called with arguments. Keys are file extensions and values are coderefs.

types

    my $types = $renderer->types;
    $renderer = $renderer->types(MojoX::Types->new);

Returns a MojoX::Types object if called without arguments. Returns the invocant if called with arguments.

root

   my $root  = $renderer->root;
   $renderer = $renderer->root('/foo/bar/templates');

Return the root file system path where templates are stored if called without arguments. Returns the invocant if called with arguments.

METHODS

MojoX::Types inherits all methods from Mojo::Base and implements the follwing the ones.

add_handler

    $renderer = $renderer->add_handler(phtml => sub { ... });

render

    my $success  = $renderer->render($c);

    $c->stash->{partial} = 1;
    my $output = $renderer->render($c);

Returns a true value if a template is successfully rendered. Returns the template output if partial is set in the stash. Returns undef if none of format, template or template_path are set in the stash. Returns undef if the template is defined, but lacks an extension and no default handler has been defined. Returns undef if the handler returns a false value. Expects a MojoX::Context object.

To determine the format to use, we first check format in the stash, and if that is empty, we check the extensions of template_path and template.

format may contain a value like html. template_path may contain an absolute path like /templates/page.html. template may contain a path relative to root, like users/list.html.

If template_path is not set in the stash, we create it by appending template to the root.

If template lacks an extension, we add one using default_format.

If format is not defined, we try to determine it from the extension of template_path.

If no handler is found for the format, we emit a warning, and check for a handler for the default_format.

A handler receives three arguments: the renderer object, the MojoX::Context object and a reference to an empty scalar, where the output can be accumulated.

If partial is defined in the stash, the output from the handler is simply returned.

Otherwise, we build our own Mojo::Message::Response and return true for success. We set the response code to 200 if none is provided, and default to text/plain if there is no type associated with the format.