AxKit2
view release on metacpan or search on metacpan
lib/AxKit2/Transformer/XSP.pm view on Meta::CPAN
Generates an attribute. The name of the attribute can either be specified
in the C<name="..."> attribute, or via a child element C<<xsp:name>>. The
value of the attribute is the text contents of the tag.
=head2 C<< <xsp:comment> >>
Normally XML comments are stripped from the output. So to add one back in
you can use the C<<xsp:comment>> tag. The contents of the tag are the
value of the comment.
=head2 C<< <xsp:text> >>
Create a plain text node. The contents of the tag are the text node to be
generated. This is useful when you wish to just generate a text node while
in an C<<xsp:logic>> section.
=head2 C<< <xsp:expr> >>
This is probably the most useful, and most important (and also the most
complex) tag. An expression is some perl code that executes, and the results
of which are added to the output. Exactly how the results are added to the
output depends very much on context.
The default method for output for an expression is as a text node. So for
example:
<p>
It is now: <xsp:expr>localtime</xsp:expr>
</p>
Will generate a text node containing the time.
If the expression is contained within an XSP namespaces, that is either a
tag in the xsp:* namespace, or a tag implementing a tag library, then an
expression generally does not create a text node, but instead is simply
wrapped in a Perl C<do {}> block, and added to the perl script. However,
there are anti-cases to this. For example if the expression is within
a C<<xsp:content>> tag, then a text node is created.
Needless to say, in every case, C<<xsp:expr>> should just "do the right
thing". If it doesn't, then something (either a taglib or XSP.pm itself)
is broken and you should report a bug.
=head1 Writing Taglibs
Writing your own taglibs can be tricky, because you're using an event
based API to write out Perl code. You may want to take a look at the
AxKit2::Transformer::XSP::TaglibHelper module, which comes with
AxKit and allows you to easily publish a taglib without writing
XML event code. Recently, another taglib helper has been developed,
AxKit2::Transformer::XSP::SimpleTaglib. The latter manages all the
details described under 'Design Patterns' for you, so you don't really
need to bother with them anymore.
A warning about character sets: All string values are passed in and
expected back as UTF-8 encoded strings. So you cannot use national characters
in a different encoding, like the widespread ISO-8859-1. This applies to
Taglib source code only. The XSP XML-source is of course interpreted according
to the XML rules. Your taglib module may want to 'use utf8;' as well, see
L<perlunicode> and L<utf8> for more information.
=head1 Design Patterns
These patterns represent the things you may want to achieve when
authoring a tag library "from scratch".
=head2 1. Your tag is a wrapper around other things.
Example:
<mail:sendmail>...</mail:sendmail>
Solution:
Start a new block, so that you can store lexical variables, and declare
any variables relevant to your tag:
in parse_start:
if ($tag eq 'sendmail') {
return '{ my ($to, $from, $sender);';
}
Often it will also be relevant to execute that code when you see the end
tag:
in parse_end:
if ($tag eq 'sendmail') {
return 'Mail::Sendmail::sendmail(
to => $to,
from => $from,
sender => $sender
); }';
}
Note there the closing of that original opening block.
=head2 2. Your tag indicates a parameter for a surrounding taglib.
Example:
<mail:to>...</mail:to>
Solution:
Having declared the variable as above, you simply set it to the empty
string, with no semi-colon:
in parse_start:
if ($tag eq 'to') {
return '$to = ""';
}
Then in parse_char:
sub parse_char {
my ($e, $text) = @_;
( run in 4.459 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )