Mojolicious::Component - Module-based Template Class
Module-based Template Base Class
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
This package provides an abstract base class for rendering derived component-based template (partials) classes.
This package has the following attributes:
controller(InstanceOf["Mojolicious::Controller"])
This attribute is read-only, accepts (InstanceOf["Mojolicious::Controller"]) values, and is optional.
(InstanceOf["Mojolicious::Controller"])
processor(InstanceOf["Mojo::Template"])
This attribute is read-only, accepts (InstanceOf["Mojo::Template"]) values, and is optional.
(InstanceOf["Mojo::Template"])
space(InstanceOf["Data::Object::Space"])
This attribute is read-only, accepts (InstanceOf["Data::Object::Space"]) values, and is optional.
(InstanceOf["Data::Object::Space"])
This package implements the following methods:
postprocess(Str $input) : Str
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.
# given: synopsis my $processed = $component->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);
preprocess(Str $input) : Str
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.
# given: synopsis my $processed = $component->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);
render(Any %args) : Str
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".
DATA
# given: synopsis my $rendered = $component->render;
# given: synopsis my $rendered = $component->render( readonly => 1, );
template(Str | Object $object = $self, Str $section = 'component') : (Any)
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.
# given: synopsis my $template = $component->template;
# given: synopsis my $template = $component->template('App::Component::Image');
# given: synopsis my $template = $component->template(App::Component::Image->new);
variables(Any %args) : (Any)
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.
# given: synopsis my $variables = { $component->variables };
# given: synopsis my $variables = { $component->variables(true => 1, false => 0) };
Al Newkirk, awncorp@cpan.org
awncorp@cpan.org
Copyright (C) 2011-2019, Al Newkirk, et al.
This is free software; you can redistribute it and/or modify it under the terms of the The Apache License, Version 2.0, as elucidated in the "license file".
Wiki
Project
Initiatives
Milestones
Contributing
Issues
To install Mojolicious::Plugin::Component, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Mojolicious::Plugin::Component
CPAN shell
perl -MCPAN -e shell install Mojolicious::Plugin::Component
For more information on module installation, please visit the detailed CPAN module installation guide.