HTML::Builder - A declarative approach to HTML generation
This document describes version 0.008 of HTML::Builder - released December 02, 2012 as part of HTML-Builder.
use HTML::Builder ':minimal'; # $html is: <div id="main"><p>Something, huh?</p></div> my $html = div { id gets 'main'; p { 'Something, huh?' } };
A quick and dirty set of helper functions to make generating small bits of HTML a little less tedious.
A unique, sorted list of the HTML tags we know about (and handle).
The actual function responsible for handling the tagging. All of the helper functions pass off to tag() (e.g. div() is sub div(&) { unshift 'div'; goto \&tag }).
div()
sub div(&) { unshift 'div'; goto \&tag }
The list of tags we think are HTML5.
The list of tags we think are HTML ( < HTML5, that is).
HTML elements considered to be deprecated.
The unique, sorted list of all tags returned by html5_tags() and html_tags().
A minimal subset of HTML5 tags as returned by html5_tags():
article aside footer header nav
Returns a HashRef of tags that conflict with Perl builtins: our named exports for the tags are the keys; the tags themselves are the values.
A list of tags related to forms, that will belong to the :form group.
:form
A list of tags related to tables, that will belong to the :table group.
:table
A list of tags we consider a "minimal" set. These will belong to the :minimal group.
:minimal
The set of all the tags we know of.
An alternative to using gets (or instances where it won't work, e.g. for gets 'name'). Takes a coderef, expects that coderef to return a list of attribute => value pairs, e.g.
gets
for gets 'name'
div { attr { id => 'one', bip => 'two', }; };
Each supported HTML tag takes a coderef, executes it, and returns the output the coderef writes to STDOUT with the return value appended.
That is:
div { say h1 { 'Hi there! }; p { "Nice day, isn't it?" } }
Generates:
<div><h1>Hi there!</h1><p>Nice day, isn't it?</p></div>
Element attributes are handled by specifying them with gets. e.g.:
div { id gets 'main'; 'Hi!' }
<div id="main">Hi!</div>
gets may be specified multiple times, for multiple attributes.
When one tag function is called from within another, the nested tag will print its output to STDOUT rather than returning it. That means that this:
div { print h1 { 'Hi there! }; p { "Nice day, isn't it?" } }
...and this:
div { h1 { 'Hi there! }; p { "Nice day, isn't it?" } }
Behave identically, from the perspective of the caller.
This package uses Sub::Exporter, so you can take advantage of the features that package provides. For example, if you wanted to import the tags in the 'minimal' group, but wanted to prefix each function with 'html_', you could do:
use HTML::Builder -minimal => { -prefix => 'html_' };
Each tag we handle is capable of being exported, and called with a coderef. This coderef is executed, and the return is wrapped in the tag. Attributes on the tag can be set from within the coderef by using gets, a la id gets 'foo'.
id gets 'foo'
By default we export the :minimal group.
Everything not conflicting with Perl builtins.
This isn't an optimal group to use as-is -- it will cause a ton of functions to be imported, including some that will conflict with several Perl builtins. If you use this group, you are highly encouraged to supply a prefix for it, like:
use HTML::Builder -all => { -prefix => 'html_' };
A basic set of the most commonly used tags:
h1..h4 div p img span script br ul ol li style a
HTML5 tags (article, header, nav, etc) -- or at least what Wikipedia thinks are HTML5 tags.
article
header
nav
The table tags:
table thead tbody tfoot tr th td
As tr would conflict with a Perl builtin, it is recommended that this group be imported with a prefix ('table_' would seem to suggest itself).
tr
Header tags:
header hgroup
Form tags:
form fieldset button input label optgroup option select textarea
This package was inspired by Template::Declare::Tags... In particular, our gets::AUTOLOAD is pretty much a straight-up copy of Template::Declare::Tags' is::AUTOLOAD, with some modifications. We also pass off to HTML::Tiny, and allow it to do the work of actually generating the output. Thanks! :)
gets::AUTOLOAD
is::AUTOLOAD
Please see those modules/websites for more information related to this module.
HTML::Tiny
Template::Declare::Tags
HTML::HTML5::Builder
Chris Weyl <cweyl@alumni.drew.edu>
Olaf Alders <olaf@wundersolutions.com>
This software is Copyright (c) 2012 by Chris Weyl.
This is free software, licensed under:
The GNU Lesser General Public License, Version 2.1, February 1999
To install HTML::Builder, copy and paste the appropriate command in to your terminal.
cpanm
cpanm HTML::Builder
CPAN shell
perl -MCPAN -e shell install HTML::Builder
For more information on module installation, please visit the detailed CPAN module installation guide.