| Text-MicroMason documentation | view source | Contained in the Text-MicroMason distribution. |
Text::MicroMason::HTMLMason - Simple Compiler for Mason-style Templating
Create a MicroMason object to interpret the templates:
use Text::MicroMason; my $mason = Text::MicroMason->new();
Use the standard compile and execute methods to parse and evalute templates:
print $mason->compile( text=>$template )->( @%args ); print $mason->execute( text=>$template, @args );
Mason syntax provides several ways to mix Perl into a text template:
<%args>
$name
</%args>
% if ( $name eq 'Dave' ) {
I'm sorry <% $name %>, I'm afraid I can't do that right now.
% } else {
<%perl>
my $hour = (localtime)[2];
my $daypart = ( $hour > 11 ) ? 'afternoon' : 'morning';
</%perl>
Good <% $daypart %>, <% $name %>!
% }
<& "includes/standard_footer.msn" &>
<%doc>
Here's a private developr comment describing this template.
</%doc>
The Text::MicroMason::HTMLMason class provides lexer and assembler methods that allow Text::MicroMason to handle most elements of HTML::Mason's template syntax.
HTML::Mason is a full-featured application server toolkit with many fatures, of which only the templating functionality is emulated.
The following sets of HTML::Mason features are supported by Text::MicroMason:
The following sets of HTML::Mason features are not supported by Text::MicroMason:
Contributed patches to add these features of HTML::Mason would be welcomed by the author. Possible implemenations are described in Text::MicroMason::ToDo.
The following internal methods are used to implement the syntax described below.
( $type, $value ) = $mason->lex_token();
Supports HTML::Mason's markup syntax.
Attempts to parse a token from the template text stored in the global $_ and returns a token type and value. Returns an empty list if unable to parse further due to an error.
Returns a hash of text elements used for Perl subroutine assembly. Used by assemble().
Supports HTML::Mason's named blocks of Perl code and documentation: %once, %init, %cleanup, and %doc.
Called by assemble(), this method provides support for Mason's <%args> blocks.
Here's an example of Mason-style templating, taken from HTML::Mason:
% my $noun = 'World';
Hello <% $noun %>!
How are ya?
Interpreting this template with Text::MicroMason produces the same output as it would in HTML::Mason:
Hello World!
How are ya?
Text::MicroMason::HTMLMason supports a syntax that is mostly a subset of that used by HTML::Mason.
The following types of markup are recognized in template pages:
Good <% (localtime)[2]>11 ? 'afternoon' : 'morning' %>.
Good <% my $h = (localtime)[2]; $h > 11 ? 'afternoon'
: 'morning' %>.
% my $daypart = (localtime)[2]>11 ? 'afternoon' : 'morning';
Good <% $daypart %>.
% if ( int rand 2 ) {
Hello World!
% } else {
Goodbye Cruel World!
% }
% #.
Good <% $ARGS{hour} >11 ? 'afternoon' : 'morning' %>.
<& "greeting.msn", hour => (localtime)[2] &>
The following types of named blocks are supported:
<%perl>
my $count = join '', map "$_... ", ( 1 .. 9 );
</%perl>
Here are some numbers: <% $count %>
Here are some numbers:
<%perl>
foreach my $digit ( 1 .. 9 ) {
</%perl>
<% $digit %>...
<%perl>
}
</%perl>
a => '...' argument pair is passed:
<%args>
$a
@b => qw( foo bar baz )
%c => ()
</%args>
Good <% $daypart %>.
<%init>
my $daypart = (localtime)[2]>11 ? 'afternoon' : 'morning';
</%init>
<%once>
my $counter = 1000;
</%once>
The count is <% ++ $counter %>.
The following types of named blocks are not supported by HTML::Mason, but are supported here as a side-effect of the way the lexer and assembler are implemented.
<% ... %> markup syntax. <%file> "greeting.msn", hour => (localtime)[2] </%file>
<& ... &> markup syntax.When Text::MicroMason::Base assembles your lexed template into the
equivalent Perl subroutine, all of the literal (non-Perl) pieces are
converted to $_out->('text'); statements, and the interpolated
expressions are converted to $_out->( do { expr } ); statements.
Code from %perl blocks and % lines are included exactly as-is.
Your code is eval'd in the Text::MicroMason::Commands package.
The use strict; pragma is enabled by default to simplify debugging.
You can create sub-templates within your template text by defining them as anonymous subroutines and then calling them repeatedly. For example, the following template will concatenate the results of the draw_item sub-template for each of three items:
<h1>We've Got Items!</h1>
% my $draw_item = sub {
<p><b><% $_[0] %></b>:<br>
<a href="/more?item=<% $_[0] %>">See more about <% $_[0] %>.</p>
% };
<%perl>
foreach my $item ( qw( Foo Bar Baz ) ) {
$draw_item->( $item );
}
</%perl>
To append to the result from within Perl code, call $_out->(text). (The $_out->() syntax is unavailable in older versions of Perl; use the equivalent &$_out() syntax instead.)
For example, the below template text will return '123456789' when it is evaluated:
<%perl>
foreach my $digit ( 1 .. 9 ) {
$_out->( $digit )
}
</%perl>
You can also directly manipulate the value @OUT, which contains the accumulating result.
For example, the below template text will return an altered version of its message if a true value for 'minor' is passed as an argument when the template is executed:
This is a funny joke.
% if ( $ARGS{minor} ) { foreach ( @OUT ) { tr[a-z][n-za-m] } }
For a full-featured web application system using this template syntax, see HTML::Mason.
For an overview of this distribution, see Text::MicroMason.
This is a subclass intended for use with Text::MicroMason::Base.
For distribution, installation, support, copyright and license information, see Text::MicroMason::Docs::ReadMe.
| Text-MicroMason documentation | view source | Contained in the Text-MicroMason distribution. |