VERSION

This document describes HTML::Tiny version 1.05

SYNOPSIS

  use HTML::Tiny;

  my $h = HTML::Tiny->new;

  # Generate a simple page
  print $h->html(
    [
      $h->head( $h->title( 'Sample page' ) ),
      $h->body(
        [
          $h->h1( { class => 'main' }, 'Sample page' ),
          $h->p( 'Hello, World', { class => 'detail' }, 'Second para' )
        ]
      )
    ]
  );

  # Outputs
  <html>
    <head>
      <title>Sample page</title>
    </head>
    <body>
      <h1 class="main">Sample page</h1>
      <p>Hello, World</p>
      <p class="detail">Second para</p>
    </body>
  </html>

DESCRIPTION

\*(C`HTML::Tiny\*(C' is a simple, dependency free module for generating \s-1HTML\s0 (and \s-1XML\s0). It concentrates on generating syntactically correct \s-1XHTML\s0 using a simple Perl notation.

In addition to the \s-1HTML\s0 generation functions utility functions are provided to

  • encode and decode \s-1URL\s0 encoded strings

  • entity encode \s-1HTML\s0

  • build query strings

  • \s-1JSON\s0 encode data structures

INTERFACE

Create a new \*(C`HTML::Tiny\*(C'. The constructor takes one optional argument: \*(C`mode\*(C'. \*(C`mode\*(C' can be either 'xml' (default) or 'html'. The difference is that in \s-1HTML\s0 mode, closed tags will not be closed with a forward slash; instead, closed tags will be returned as single open tags. Example: # Set HTML mode. my $h = HTML::Tiny->new( mode => 'html' );

# The default is XML mode, but this can also be defined explicitly. $h = HTML::Tiny->new( mode => 'xml' ); \s-1HTML\s0 is a dialect of \s-1SGML\s0, and is not \s-1XML\s0 in any way. \*(L"Orphan\*(R" open tags or unclosed tags are legal and in fact expected by user agents. In practice, if you want to generate \s-1XML\s0 or \s-1XHTML\s0, supply no arguments. If you want valid \s-1HTML\s0, use \*(C`mode => 'html'\*(C'.

\s-1HTML\s0 Generation

Returns \s-1HTML\s0 (or \s-1XML\s0) that encloses each of the arguments in the specified tag. For example print $h->tag('p', 'Hello', 'World'); would print <p>Hello</p><p>World</p> notice that each argument is individually wrapped in the specified tag. To avoid this multiple arguments can be grouped in an anonymous array: print $h->tag('p', ['Hello', 'World']); would print <p>HelloWorld</p> The [ and ] can be thought of as grouping a number of arguments. Attributes may be supplied by including an anonymous hash as the first element in the argument list (after the tag name): print $h->tag('p', { class => 'normal' }, 'Foo'); would print <p class="normal">Foo</p> Attribute values will be \s-1HTML\s0 entity encoded as necessary. Multiple hashes may be supplied in which case they will be merged: print $h->tag('p', { class => 'normal' }, 'Bar', { style => 'color: red' }, 'Bang!' ); would print <p class="normal">Bar</p><p class="normal" style="color: red">Bang!</p> Notice that the class=\*(L"normal\*(R" attribute is merged with the style attribute for the second paragraph. To remove an attribute set its value to undef: print $h->tag('p', { class => 'normal' }, 'Bar', { class => undef }, 'Bang!' ); would print <p class="normal">Bar</p><p>Bang!</p> An empty attribute - such as 'checked' in a checkbox can be encoded by passing an empty array reference: print $h->closed( 'input', { type => 'checkbox', checked => [] } ); would print <input checked type="checkbox" /> Return Value In a scalar context \*(C`tag\*(C' returns a string. In a list context it returns an array each element of which corresponds to one of the original arguments: my @html = $h->tag('p', 'this', 'that'); would return @html = ( '<p>this</p>', '<p>that</p>' ); That means that when you nest calls to tag (or the equivalent \s-1HTML\s0 aliases - see below) the individual arguments to the inner call will be tagged separately by each enclosing call. In practice this means that print $h->tag('p', $h->tag('b', 'Foo', 'Bar')); would print <p><b>Foo</b></p><p><b>Bar</b></p> You can modify this behavior by grouping multiple args in an anonymous array: print $h->tag('p', [ $h->tag('b', 'Foo', 'Bar') ] ); would print <p><b>Foo</b><b>Bar</b></p> This behaviour is powerful but can take a little time to master. If you imagine '[' and ']' preventing the propagation of the 'tag individual items' behaviour it might help visualise how it works. Here's an \s-1HTML\s0 table (using the tag-name convenience methods - see below) that demonstrates it in more detail: print $h->table( [ $h->tr( [ $h->th( 'Name', 'Score', 'Position' ) ], [ $h->td( 'Therese', 90, 1 ) ], [ $h->td( 'Chrissie', 85, 2 ) ], [ $h->td( 'Andy', 50, 3 ) ] ) ] ); which would print the unformatted version of: <table> <tr><th>Name</th><th>Score</th><th>Position</th></tr> <tr><td>Therese</td><td>90</td><td>1</td></tr> <tr><td>Chrissie</td><td>85</td><td>2</td></tr> <tr><td>Andy</td><td>50</td><td>3</td></tr> </table> Note how you don't need a td() for every cell or a tr() for every row. Notice also how the square brackets around the rows prevent tr() from wrapping each individual cell. Often when generating nested \s-1HTML\s0 you will find yourself writing corresponding nested calls to \s-1HTML\s0 generation methods. The table generation code above is an example of this. If you prefer these nested method calls can be deferred like this: print $h->table( [ \'tr', [ \'th', 'Name', 'Score', 'Position' ], [ \'td', 'Therese', 90, 1 ], [ \'td', 'Chrissie', 85, 2 ], [ \'td', 'Andy', 50, 3 ] ] ); In general a nested call like $h->method( args ) may be rewritten like this [ \'method', args ] This allows complex \s-1HTML\s0 to be expressed as a pure data structure. See the \*(C`stringify\*(C' method for more information. Generate an opening \s-1HTML\s0 or \s-1XML\s0 tag. For example: print $h->open('marker'); would print <marker> Attributes can be provided in the form of anonymous hashes in the same way as for \*(C`tag\*(C'. For example: print $h->open('marker', { lat => 57.0, lon => -2 }); would print <marker lat="57.0" lon="-2"> As for \*(C`tag\*(C' multiple attribute hash references will be merged. The example above could be written: print $h->open('marker', { lat => 57.0 }, { lon => -2 }); Generate a closing \s-1HTML\s0 or \s-1XML\s0 tag. For example: print $h->close('marker'); would print: </marker> Generate a closed \s-1HTML\s0 or \s-1XML\s0 tag. For example print $h->closed('marker'); would print: <marker /> As for \*(C`tag\*(C' and \*(C`open\*(C' attributes may be provided as hash references: print $h->closed('marker', { lat => 57.0 }, { lon => -2 }); would print: <marker lat="57.0" lon="-2" /> Calls either \*(C`tag\*(C' or \*(C`closed\*(C' based on built in rules for the tag. Used internally to implement the tag-named methods. Called internally to obtain string representations of values. It also implements the deferred method call notation (mentioned above) so that my $table = $h->table( [ $h->tr( [ $h->th( 'Name', 'Score', 'Position' ) ], [ $h->td( 'Therese', 90, 1 ) ], [ $h->td( 'Chrissie', 85, 2 ) ], [ $h->td( 'Andy', 50, 3 ) ] ) ] ); may also be written like this: my $table = $h->stringify( [ \'table', [ \'tr', [ \'th', 'Name', 'Score', 'Position' ], [ \'td', 'Therese', 90, 1 ], [ \'td', 'Chrissie', 85, 2 ], [ \'td', 'Andy', 50, 3 ] ] ] ); Any reference to an array whose first element is a reference to a scalar [ \'methodname', args ] is executed as a call to the named method with the specified args.

Methods named after tags

In addition to the methods described above \*(C`HTML::Tiny\*(C' provides all of the following \s-1HTML\s0 generation methods:

a abbr acronym address area b base bdo big blockquote body br button caption cite code col colgroup dd del div dfn dl dt em fieldset form frame frameset h1 h2 h3 h4 h5 h6 head hr html i iframe img input ins kbd label legend li link map meta noframes noscript object ol optgroup option p param pre q samp script select small span strong style sub sup table tbody td textarea tfoot th thead title tr tt ul var

The following methods generate closed \s-1XHTML\s0 (<br />) tags by default:

area base br col frame hr img input meta param

So:

print $h->br; # prints <br /> print $h->input({ name => 'field1' }); # prints <input name="field1" /> print $h->img({ src => 'pic.jpg' }); # prints <img src="pic.jpg" />

All other tag methods generate tags to wrap whatever content they are passed:

print $h->p('Hello, World');

prints:

<p>Hello, World</p>

So the following are equivalent:

print $h->a({ href => 'http://hexten.net' }, 'Hexten');

and

print $h->tag('a', { href => 'http://hexten.net' }, 'Hexten');

Utility Methods

\s-1URL\s0 encode a string. Spaces become '+' and non-alphanumeric characters are encoded as '%' + their hexadecimal character code. $h->url_encode( ' <hello> ' ) # returns '+%3chello%3e+' \s-1URL\s0 decode a string. Reverses the effect of \*(C`url_encode\*(C'. $h->url_decode( '+%3chello%3e+' ) # returns ' <hello> ' Generate a query string from an anonymous hash of key, value pairs: print $h->query_encode({ a => 1, b => 2 }) would print a=1&b=2 Encode the characters '<', '>', '&', '\'' and '"' as their \s-1HTML\s0 entity equivalents: print $h->entity_encode( '<>\'"&' ); would print: <>'"& Encode a data structure in \s-1JSON\s0 (Javascript) format: print $h->json_encode( { ar => [ 1, 2, 3, { a => 1, b => 2 } ] } ); would print: {"ar":[1,2,3,{"a":1,"b":2}]} Because \s-1JSON\s0 is valid Javascript this method can be useful when generating ad-hoc Javascript. For example my $some_perl_data = { score => 45, name => 'Fred', history => [ 32, 37, 41, 45 ] };

# Transfer value to Javascript print $h->script( { type => 'text/javascript' }, "\nvar someVar = " . $h->json_encode( $some_perl_data ) . ";\n " );

# Prints # <script type="text/javascript"> # var someVar = {"history":[32,37,41,45],"name":"Fred","score":45}; # </script> If you attempt to json encode a blessed object \*(C`json_encode\*(C' will look for a \*(C`TO_JSON\*(C' method and, if found, use its return value as the structure to be converted in place of the object. An attempt to encode a blessed object that does not implement \*(C`TO_JSON\*(C' will fail.

Subclassing

An \*(C`HTML::Tiny\*(C' is a blessed hash ref. Subclass \*(C`validate_tag\*(C' to throw an error or issue a warning when an attempt is made to generate an invalid tag.

CONFIGURATION AND ENVIRONMENT

HTML::Tiny requires no configuration files or environment variables.

DEPENDENCIES

By design HTML::Tiny has no non-core dependencies.

To run the tests you will require Test::More.

INCOMPATIBILITIES

None reported.

BUGS AND LIMITATIONS

No bugs have been reported.

Please report any bugs or feature requests to \*(C`[email protected]\*(C', or through the web interface at <http://rt.cpan.org>.

AUTHOR

Andy Armstrong \*(C`<[email protected]>\*(C'

Aristotle Pagaltzis \*(C`<[email protected]>\*(C'

LICENCE AND COPYRIGHT

Copyright (c) 2008, Andy Armstrong \*(C`<[email protected]>\*(C'. All rights reserved.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See perlartistic.

DISCLAIMER OF WARRANTY

\s-1BECAUSE\s0 \s-1THIS\s0 \s-1SOFTWARE\s0 \s-1IS\s0 \s-1LICENSED\s0 \s-1FREE\s0 \s-1OF\s0 \s-1CHARGE\s0, \s-1THERE\s0 \s-1IS\s0 \s-1NO\s0 \s-1WARRANTY\s0 \s-1FOR\s0 \s-1THE\s0 \s-1SOFTWARE\s0, \s-1TO\s0 \s-1THE\s0 \s-1EXTENT\s0 \s-1PERMITTED\s0 \s-1BY\s0 \s-1APPLICABLE\s0 \s-1LAW\s0. \s-1EXCEPT\s0 \s-1WHEN\s0 \s-1OTHERWISE\s0 \s-1STATED\s0 \s-1IN\s0 \s-1WRITING\s0 \s-1THE\s0 \s-1COPYRIGHT\s0 \s-1HOLDERS\s0 \s-1AND/OR\s0 \s-1OTHER\s0 \s-1PARTIES\s0 \s-1PROVIDE\s0 \s-1THE\s0 \s-1SOFTWARE\s0 \*(L"\s-1AS\s0 \s-1IS\s0\*(R" \s-1WITHOUT\s0 \s-1WARRANTY\s0 \s-1OF\s0 \s-1ANY\s0 \s-1KIND\s0, \s-1EITHER\s0 \s-1EXPRESSED\s0 \s-1OR\s0 \s-1IMPLIED\s0, \s-1INCLUDING\s0, \s-1BUT\s0 \s-1NOT\s0 \s-1LIMITED\s0 \s-1TO\s0, \s-1THE\s0 \s-1IMPLIED\s0 \s-1WARRANTIES\s0 \s-1OF\s0 \s-1MERCHANTABILITY\s0 \s-1AND\s0 \s-1FITNESS\s0 \s-1FOR\s0 A \s-1PARTICULAR\s0 \s-1PURPOSE\s0. \s-1THE\s0 \s-1ENTIRE\s0 \s-1RISK\s0 \s-1AS\s0 \s-1TO\s0 \s-1THE\s0 \s-1QUALITY\s0 \s-1AND\s0 \s-1PERFORMANCE\s0 \s-1OF\s0 \s-1THE\s0 \s-1SOFTWARE\s0 \s-1IS\s0 \s-1WITH\s0 \s-1YOU\s0. \s-1SHOULD\s0 \s-1THE\s0 \s-1SOFTWARE\s0 \s-1PROVE\s0 \s-1DEFECTIVE\s0, \s-1YOU\s0 \s-1ASSUME\s0 \s-1THE\s0 \s-1COST\s0 \s-1OF\s0 \s-1ALL\s0 \s-1NECESSARY\s0 \s-1SERVICING\s0, \s-1REPAIR\s0, \s-1OR\s0 \s-1CORRECTION\s0.

\s-1IN\s0 \s-1NO\s0 \s-1EVENT\s0 \s-1UNLESS\s0 \s-1REQUIRED\s0 \s-1BY\s0 \s-1APPLICABLE\s0 \s-1LAW\s0 \s-1OR\s0 \s-1AGREED\s0 \s-1TO\s0 \s-1IN\s0 \s-1WRITING\s0 \s-1WILL\s0 \s-1ANY\s0 \s-1COPYRIGHT\s0 \s-1HOLDER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