NAME
HTML::Make::Page - Automate making HTML <head> and <body>
SYNOPSIS
use HTML::Make::Page 'make_page';
my ($html, $body) = make_page (title => 'Personal Home Page', lang => 'en');
$body->add_text (<<EOF);
<img src='under-construction.gif' alt='This site is ALWAYS under construction!!!'>
<p>Personal details</p>
EOF
print $html->text ();
produces output
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta content="width=device-width, initial-scale=1.0" name="viewport">
<title>Personal Home Page</title>
</head>
<body>
<img src='under-construction.gif' alt='This site is ALWAYS under construction!!!'>
<p>Personal details</p>
</body>
</html>
(This example is included as synopsis.pl in the distribution.)
VERSION
This documents version 0.02 of HTML-Make-Page corresponding to git commit 4eaaced079d780001a6dd1b4c12b1bb39a071554 released on Tue Apr 6 20:22:23 2021 +0900.
DESCRIPTION
This module simplifies generating the <head> and <body> elements of an HTML page using HTML::Make.
Restrictions
The output is restricted to the following variants of HTML:
- HTML 5 only
-
The output has a doctype of
<!DOCTYPE html>
. -
No <HTML> etc., the tags generated are lower-case only.
- UTF-8 encoding only
-
The output has
<meta charset='UTF-8'>
added automatically. - Viewport is added automatically
-
The viewport meta element, necessary for viewing web pages on mobile phones, is automatically added to the output.
-
XHTML tags of the form
<br/>
are not necessary in HTML 5 and are not supported.
FUNCTIONS
make_page
my ($html, $body) = make_page (title => 'My Cool Page');
The argument to the function is a hash of options of the form make_page (%options)
. The options are as follows.
Options
- css
-
An array reference giving a list of style sheets to be added using <link> in <head>.
my ($h, $b) = make_page (css => ['my.css', 'your.css']);
- js
-
An array reference of names of JavaScript files you want to include in your header. To add
async
ordefer
to the script tag, use a hash reference in place of the name of the script with the keyssrc
for the file and then any true value forasync
ordefer
to add those.To add JavaScript as text, use a field
text
.my ($h, $b) = make_page (js => ['cat.js', 'dog.js', {src => 'parrot.js', async => 1}]);
- lang
-
The language of your page, for example "en" for English. This is added to the opening <html> tag.
my ($h, $b) = make_page (lang => "en");
- link
-
Link elements. The value must be an array reference containing hash references. Each hash reference must contain the compulsory
rel
key, otherwise it is not included in the output.If no
href
is present, a warning is printed. This warning may be turned off with "quiet".CSS stylesheets can be added using this, but they can also be added more simply using the "css" option.
my ($h, $b) = make_page (link => [{rel=>"icon", type=>"image/png", href=>"favicon.png"}]);
- meta
-
Meta-tag things which you want to add, for example author, description or keywords meta-tags. The value must be an array reference containing hash references.
Note that the
viewport
meta tag and thecharset
meta tag are included by default.my ($h, $b) = make_page (meta => [{name => 'author', content => 'Long John Silver'}]);
- quiet
-
Suppress warnings about harmless things like omitted titles and unknown options.
my ($h, $b) = make_page (quiet => 1);
- style
-
CSS in text format. The starting and closing <style> html tags will be added for you.
my $style = <<EOF; h1 { color: white; background-color: #FFF; } EOF my ($h, $b) = make_page (style => $style);
- title
-
The title of your page. A <title> element is "compulsory" in HTML so if you omit this a warning is printed. Suppress the warning with "quiet".
my ($h, $b) = make_page (title => "My Cool Page");
Examples
Inline JavaScript in the header
This puts JavaScript inline into the header and then calls the JavaScript using onload
on the <body>
HTML element.
use utf8;
use HTML::Make::Page 'make_page';
my $jstext = <<EOF;
function love() {
alert ("💕 I love you baby 🥰");
}
EOF
my ($h, $b) = make_page (js => [{text => $jstext}], title => '💌');
$b->add_attr (onload => 'love ();');
print $h->text ();
produces output
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta content="width=device-width, initial-scale=1.0" name="viewport">
<script>
function love() {
alert ("💕 I love you baby 🥰");
}
</script>
<title>💌</title>
</head>
<body onload="love ();">
</body>
</html>
(This example is included as js-text.pl in the distribution.)
DEPENDENCIES
- HTML::Make
-
This module is based on HTML::Make.
AUTHOR
Ben Bullock, <bkb@cpan.org>
COPYRIGHT & LICENCE
This package and associated files are copyright (C) 2021 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.