Mason
view release on metacpan or search on metacpan
lib/Mason/Manual/Components.pod view on Meta::CPAN
You can add other methods that output HTML via the C<< <$method> >> section;
these methods automatically have access to C<$self> and C<$m>.
<%method leftcol>
<table><tr>
<td><% $foo %></td>
...
</tr></table>
</%method>
...
<% # call leftcol method and insert HTML here %>
<% $.leftcol %>
Methods can also take argument lists:
<%method list ($style, $items)>
<ul style="<% $style %>">
% foreach my $item (@$items) {
...
% }
</ul>
</%method>
Both C<main> and other methods defined with C<< <%method> >> automatically get
a C<< return undef >> at their end, so that they don't accidentally return
values.
Pure-Perl methods that return a value can be added within the << <%class> >>
section.
<%class>
method multiply ($a, $b) {
return $a * $b;
}
</%class>
...
<%init>
my $value = $.multiply(5, 6);
</%init>
Note that L<Method::Signatures::Simple> provides the C<method> keyword and
argument lists; this is used throughout Mason internals as well. If you prefer
straight-up Perl subroutines:
<%class>
sub multiply {
my ($self, $a, $b) = @_;
return $a * $b;
}
</%class>
=head2 Output versus return value
Most Mason methods output content such as HTML. The content is not actually
returned, but is instead appended to an implicit buffer. This is slightly more
complicated but is necessary for supporting streaming applications.
When Mason generates C<main> and other methods declared with C<< <%method> >>,
it puts an implicit
return undef;
at the bottom of the method, so that unless you specify otherwise, there will
be no return value. This is important because of syntactical shortcuts like
<% inner() %>
<% $.leftcol %>
which would (undesirably) print the return value if it existed.
=head1 INHERITANCE
Each component class naturally inherits from (or 'extends') a superclass. The
default superclass for components is L<Mason::Component|Mason::Component>, but
this may be overridden in two ways: the I<extends flag> and I<autobase
components>.
=head2 Extends flag
A component can declare its superclass via the C<extends> flag:
<%flags>
extends => '/some/other/component'
</%flags>
The path may be absolute as shown above, or relative to the component's path.
Note that including a raw C<extends> keyword in a C<< <%class> >> section will
not work reliably.
=head2 Autobase components
Autobase components are specially named components that automatically become
the superclass of all components in their directory and subdirectories. The
default names are "Base.mp" and "Base.mc" - you can customize this with the
C<autobase_names> parameter.
For example, in this directory hierarchy,
Base.mp
main.mc
colors/
red.mc
blue.mc
flavors/
Base.mc
vanilla.mc
chocolate.mc
assuming that no components have C<extends> flags,
=over
=item *
/Base.mp is the superclass of /main.mc, /colors/red.mc, /colors/blue.mc, and
( run in 0.429 second using v1.01-cache-2.11-cpan-d06a3f9ecfd )