NAME
HTML::Make - A flexible HTML generator
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>');
# Get the output
print $table->text ();
outputs
<table><tr><td>This is your cell.<a href="http://www.example.org/">Example</a></td>
</tr>
</table>
VERSION
This documents HTML::Make version 0.06 corresponding to git commit 9bd1ebbc0f95af57e6885dbde4025e8709ba9dd4 released on Mon Nov 28 13:37:29 2016 +0900.
DESCRIPTION
An HTML generator. Make a top node using "new"
my $top_node = HTML::Make->new ('ul');
then add child elements to the top node using "push":
my $element = $top_node->push ('li');
Add text to elements using "add_text":
$element->add_text ('Ça plane pour moi');
Add attributes to elements using "add_attr":
$element->add_attr (class => 'plastic bertrand');
Get the HTML as text with "text" on the top node:
my $html = $top_node->text ();
Add HTML to an element using "add_text":
$element->add_text ("<p>This is a paragraph with <i>italic</i> text.</p>");
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 uses the list of known HTML tags from "HTML::Valid::Tagset", 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 the nocheck
option:
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.
This issues a warning if the attribute is not valid for the tag, according to the tag_attr_ok
method of "HTML::Valid::Tagset". This is restricted to what is valid in HTML5.
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>
The return value is the added text object.
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/'});
This warns if you add some types of elements to possibly inappropriate parent elements. For example if you add an <li> tag to a <tr> or something.
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
.
use HTML::Make;
my $ol = HTML::Make->new ('ol');
$ol->multiply ('li', ['one', 'two', 'three']);
print $ol->text ();
outputs
<ol><li>one</li>
<li>two</li>
<li>three</li>
</ol>
BUGS
This module assumes you want to make HTML5.
There is no way to control the whitespace in the output HTML.
There is no way to add HTML comments to the output except by adding them as text.
DEPENDENCIES
This module depends on the following Perl modules.
- Carp
-
Carp is used to report errors.
- HTML::Valid::Tagset
-
HTML::Valid::Tagset is used to validate tags and tag/attribute pairs.
SEE ALSO
- CGI
-
The CGI module contains HTML generation, although it is "deprecated". Unfortunately very much more effort has gone into telling you about why you should not use CGI's HTML generation than has gone into suggesting what you should use instead.
- HTML::Generator
-
This project is on the SourceForge site, not CPAN. I have not tested it.
- HTML::Native
- HTML::TagTree
- HTML::Template
- HTML::Tiny
-
This is similar to the HTML generation which is in CGI. Its last update, version 1.05, was in 2009, and it doesn't include HTML5 tags.
AUTHOR
Ben Bullock, <bkb@cpan.org>
COPYRIGHT & LICENCE
This package and associated files are copyright (C) 2012-2016 Ben Bullock.
You can use, copy, modify and redistribute this package and associated files under the Perl Artistic Licence or the GNU General Public Licence.