NAME

HTML::Make - make HTML

SYNOPSIS

# Make a table.
use HTML::Make;
my $table = HTML::Make->new ('table');
my $tr = $table->push ('tr');
my $td = $tr->push ('td', text => 'This is your cell.');
# Add HTML as text.
$td->add_text ('<a href="http://www.example.org/">Example</a>');
print $table->text ();

DESCRIPTION

This is an HTML generator.

METHODS

new

my $element = HTML::Make->new ('li');

Make a new HTML element of the specified type.

It is possible to add attributes or text to the item. To add attributes, use the following syntax:

my $element = HTML::Make->new ('li', attr => {class => 'biglist'});

To add text,

my $element = HTML::Make->new ('li', text => "White punks on dope");

Both attributes and text may be added:

my $element = HTML::Make->new ('li', attr => {id => 'ok'}, text => 'OK');

HTML::Make has a list of known HTML tags and will issue a warning if the type given as the first argument to new is not on its list of tags. To switch off this behaviour, use

my $freaky = HTML::Make->new ('freaky', nocheck => 1);

add_attr

$obj->add_attr (class => 'buggles');

Add attributes to the specified object, in other words

my $obj = HTML::Make->new ('li');
$obj->add_attr (class => 'beano');
my $obj_text = $obj->text ();
# <li class="beano"></li>

This issues a warning of the form "Overwriting attribute 'class' for 'li'" if the object already contains an attribute of the specified type.

add_text

$element->add_text ('buggles');

Add text to $element. For example,

my $element = HTML::Make->new ('p');
$element->add_text ('peanuts');
print $element->text ();
# <p>peanuts</p>

The text may contain HTML elements:

my $element = HTML::Make->new ('p');
$element->add_text ('peanuts <i>eggs</i>');
print $element->text ();
# <p>peanuts <i>eggs</i></p>

push

my $child = $element->push ('tag');

Add child element of type <tag> to $element and return the result as a new HTML::Make object. For example,

my $table = HTML::Make->new ('table');
my $row = $table->push ('tr');
my $cell = $row->push ('td');
print $table->text ();
# <table><tr><td></td></tr></table>

It's also possible to add all of the same arguments as "new", for example

$element->push ('a', attr => {href => 'http://www.example.org/'});

opening_tag

my $tag = $obj->opening_tag ();

Returns the text value of the HTML tag opening, complete with attributes.

text

$element->text ();

This function returns the element as text.

my $p = HTML::Make->new ('p');
print $p->text ();
# <p></p>

multiply

my @elements = $obj->multiply ('li', \@contents);

Create multiple child elements of $obj of type given by the first argument, with text contents given by \@contents.

my $ol = HTML::Make->new ('ol');
$ol->multiply ('li', ['one', 'two', 'three']);
print $ol->text ();
# <ol><li>one</li>\n<li>two</li>\n<li>three</li>\n</ol>

COPYRIGHT AND LICENCE

Copyright (c) 2013 Ben Bullock. This Perl module may be used, redistributed, modified, and copied under the same terms as Perl itself.