Mojolicious::Component

Module-based Template Class

Module-based Template Base Class

method: preprocess method: postprocess method: render method: template method: variables

package App::Component::Image;

use Mojo::Base 'Mojolicious::Component';

has alt => 'random';
has height => 126;
has width => 145;
has src => '/random.gif';

1;

# __DATA__
#
# @@ component
#
# <img
#   alt="<%= $component->alt %>"
#   height="<%= $component->height %>"
#   src="<%= $component->src %>"
#   width="<%= $component->width %>"
# />

package main;

my $component = App::Component::Image->new;

# $component->render

controller: ro, opt, InstanceOf["Mojolicious::Controller"] space: ro, opt, InstanceOf["Data::Object::Space"] processor: ro, opt, InstanceOf["Mojo::Template"]

This package provides an abstract base class for rendering derived component-based template (partials) classes.

The preprocess method expects a template string. This method is called automatically before "postprocess", after locating the template in the class hierarchy, acting as a before (template loading) hook.

preprocess(Str $input) : Str

=example-1 preprocess

# given: synopsis

my $processed = $component->preprocess('');

=example-2 preprocess

package App::Component::Left::ImageLink;

use Mojo::Base 'App::Component::Image';

sub preprocess {
  my ($self, $input) = @_;
  return '<a href="/">' . $input . '</a>';
}

package main;

my $component = App::Component::Left::ImageLink->new;

my $processed = $component->preprocess($component->template);

The postprocess method expects a template string. This method is called automatically after and passed the results of the "preprocess" method, and its results are passed to the "render" method, acting as an after (template loading) hook.

postprocess(Str $input) : Str

=example-1 postprocess

# given: synopsis

my $processed = $component->postprocess('');

=example-2 postprocess

package App::Component::Right::ImageLink;

use Mojo::Base 'App::Component::Image';

sub postprocess {
  my ($self, $input) = @_;
  return '<a href="/">' . $input . '</a>';
}

package main;

my $component = App::Component::Right::ImageLink->new;

my $processed = $component->postprocess($component->template);

The render method loads the component template string data from the DATA section of the component class and renders it using the Mojo::Template object available via "processor".

render(Any %args) : Str

=example-1 render

# given: synopsis

my $rendered = $component->render;

=example-2 render

# given: synopsis

my $rendered = $component->render(
  readonly => 1,
);

The template method is used to load template strings from the DATA section of the class or object specified. The instance invocant will be used if no specific class or object is presented. If an object is provided but no DATA section exists, the object's class hierarchy will be searched returning the first superclass with a matching data section.

template(Str | Object $object = $self, Str $section = 'component') : (Any)

=example-1 template

# given: synopsis

my $template = $component->template;

=example-2 template

# given: synopsis

my $template = $component->template('App::Component::Image');

=example-3 template

# given: synopsis

my $template = $component->template(App::Component::Image->new);

The variables method is called automatically during template rendering and its return value, assumed to be key-value pairs, are passed to the template rendering method as template variables. Any key-value pairs passed to the "render" method will be passed to this method making this method, if overridden, the ideal place to set component template variable defaults and/or override existing variables.

variables(Any %args) : (Any)

=example-1 variables

# given: synopsis

my $variables = {
  $component->variables
};

=example-2 variables

# given: synopsis

my $variables = {
  $component->variables(true => 1, false => 0)
};

18 POD Errors

The following errors were encountered while parsing the POD:

Around line 10:

Unknown directive: =name

Around line 16:

Unknown directive: =tagline

Around line 22:

Unknown directive: =abstract

Around line 28:

Unknown directive: =includes

Around line 38:

Unknown directive: =synopsis

Around line 70:

Unknown directive: =attributes

Around line 78:

Unknown directive: =description

Around line 85:

Unknown directive: =method

Around line 91:

Unknown directive: =signature

Around line 120:

Unknown directive: =method

Around line 127:

Unknown directive: =signature

Around line 155:

=cut found outside a pod block. Skipping to next block.

Around line 157:

Unknown directive: =method

Around line 163:

Unknown directive: =signature

Around line 183:

Unknown directive: =method

Around line 191:

Unknown directive: =signature

Around line 215:

Unknown directive: =method

Around line 224:

Unknown directive: =signature