HTML::HTML5::Builder(3pm) | User Contributed Perl Documentation | HTML::HTML5::Builder(3pm) |
HTML::HTML5::Builder - erect some scaffolding for your documents
use HTML::HTML5::Builder qw[:standard JQUERY]; open my $fh, '<', 'inline-script.js'; print html( -lang => 'en', head( title('Test'), Meta(-charset => 'utf-8'), ), body( h1('Test'), p('This is a test.'), JQUERY(-version => '1.6.4'), script(-type => 'text/javascript', $fh), ), );
This module can export function names corresponding to any HTML5 element.
Each function returns an XML::LibXML::Element with the same name as the function. The arguments to each function are processed as a list, and used to set the attributes and contents of that element.
For each item on the list:
p('-class', 'warning', '$LordLucan not found.');
In this example, a paragraph element is returned, with the class attribute set to 'warning' and the textual contents '$LordLucan not found.'.
Sometimes it's necessary to protect values against this guesswork. By passing a hashref, all the keys and values are interpreted as setting attributes; by passing an arrayref, all values are interpreted as setting the contents of the element.
p(['-class'], { warning => '$LordLucan not found.' });
In this example, a paragraph element is returned, with the warning attribute set to '$LordLucan not found.' and the textual contents '-class'.
no warnings 'HTML::HTML::Builder';
The "html" function does not return an "XML::LibXML::Element", but rather a "HTML::HTML5::Builder::Document" object.
There is special handling for "time" (or "Time"). If the first parameter passed to it is a DateTime object, then that object is used to set its datetime attribute. If there are no subsequent parameters, then the stringified form of the object is also used to form the content of the <time> element.
Note that the functions that generate <meta>, <link>, <q>, <time>, <sub>, <s> and <map> HTML elements are named "Meta()", "Link()", "Q()", "Time()", "Sub()", "S()" and "Map()" respectively, with an upper-case first letter. This is because each of these names corresponds to a built-in perl keyword (except meta, which is used by Moose). The lower-case versions of these do exist, and can be exported if you ask for them explicitly. The lower-case versions are also available as methods using the object-oriented syntax. (In fact, lower case and ucfirst versions exist for all HTML elements - they're just not always exportable.)
This should be a so-called "balanced chunk". Due to limitations in HTML::HTML5::Parser, this only works for body content. Croaks if HTML::HTML5::Parser is not installed.
html( head( title('Funny test'), ), body( h1('Funny test'), RAW_CHUNK("<p>Here's a fish: <=><"), ), );
A processing instruction is used to represent this data in the DOM. HTML::HTML5::Writer can detect that processing instruction and use it to output the raw data. If you're not using HTML::HTML5::Writer to serialise the document, then you may need to post-process the serialised document.
With great power comes great responsibility.
There are also a number of functions that create lists of multiple HTML elements, for boiler-plate code.
Other options include -source, to indicate where to link to jQuery (currently allowed values are "Google", "Microsoft" and "official"); and -min, a boolean which indicates whether the minified version should be linked to (true by default).
Setting option -ui to true, also includes jQuery UI. A version number can be indicated using -ui_version. A theme can be included setting -theme. Setting either -ui_version or -theme will imply -ui.
JQUERY( -source => 'official', -version => '1.6.4', -ui_version => '1.8.16', -theme => 'eggplant', );
If versions aren't provided, defaults to the latest versions of the libraries that the author of HTML::HTML5::Builder was aware of at the time of publication. If you choose a version which is known to be unavailable at the selected CDN, the function should automatically choose a slightly later version.
Other options supported are:
OPENGRAPH( -title => "Hello World", -type => "example", -description => "A global greeting.", );
None by default. Pretty much anything can be exported on request.
Export tags:
You can also use these functions as methods of an object blessed into the HTML::HTML5::Builder package.
my $b = HTML::HTML5::Builder->new; my $document = $b->html( -lang => 'en', $b->head( $b->title('Test', \(my $foo)), $b->meta(-charset => 'utf-8'), ), $b->body( $b->h1('Test'), $b->p('This is a test.') ), );
RDF::RDFa::Generator has a "nodes" method which returns a handy list of "XML::LibXML::Node" objects.
use DateTime; use HTML::HTML5::Builder qw[:standard]; use RDF::RDFa::Generator; use RDF::Trine; my $url = 'http://dbpedia.org/data/Charles_Darwin'; my $model = RDF::Trine::Model->new; RDF::Trine::Parser->parse_url_into_model($url, $model); my $gen = RDF::RDFa::Generator->new(style=>'HTML::Pretty'); print html( head( title("Some Data About Charles Darwin"), ), body( h1("Some Data About Charles Darwin"), $gen->nodes($model), hr(), address( "Source: $url", br(), "Generated: ", Time(DateTime->now), ), ), );
Nice?
HTML::HTML5::Builder doesn't nicely indent your markup, but XML::LibXML::PrettyPrint can.
use HTML::HTML5::Builder qw(:standard); use XML::LibXML::PrettyPrint qw(print_xml); print_xml html( head(title("Test")), body(h1("Test"), p("This is a test.")), );
Please report any bugs to <http://rt.cpan.org/Dist/Display.html?Queue=HTML-HTML5-Builder>.
XML::LibXML, HTML::HTML5::Writer, HTML::HTML5::Builder::Document.
Toby Inkster <tobyink@cpan.org>.
This software is copyright (c) 2011 by Toby Inkster.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
2022-06-14 | perl v5.34.0 |