The content of the <head>
section usually remains
widely unaltered across all HTML files of a website. The vantage of
keeping the constant part in a single separate file is obvious. To
realize this with UHTML we need to
create an <include>
tag
that inserts the content of one file called head-data
into a html
file. We assume
that we have an include
directory in our document
root where we keep our overlapping chunks of HTML code in single files.
Assuming UHTML is installed on our system, the UHTML file could then look like this:
index.uhtml <html> <head> <include file="/include/head-data"> ... </head> ... </html>
The functionality of the <include>
tag is an
asset in many projects. Following the mentioned
naming convention proposal the perl code is placed in a file named
include.pm
located in the
subdirectory UHTML of the script (CGI) directory. The file
include.pm
could then look like that:
include.pm use uHTML ; sub Include($) { my $Node = shift; $Node->map(join('',<FH>),'') if $Node->Attr('file') and open FH,$ENV{'DOCUMENT_ROOT'}.$Node->Attr('file'); } uHTML->registerTag('include',\&Include);
To link this small library with our website a CGI hook is needed.
We place the necessary code in a file called
uhtml.pl
in the script directory:
uhtml.pl #!/usr/bin/perl use uHTML; open $FILE,"$ENV{'DOCUMENT_ROOT'}$ENV{'PATH_INFO'}" or die "File: $ENV{'PATH_INFO'} not found"; print "Content-type: text/html\n\n"; print uHTML::recode(join('',<$FILE>));
To get the functionality “magically” into all
*.uhtml
files, we add some lines into the
.htaccess
file in our DOCUMENT_ROOT
:
.htaccess DirectoryIndex index.uhtml RewriteEngine on RewriteRule ^(/?)(.*\.uhtml) $1cgi-bin/uhtml.pl/$2 [L,QSA]
This full working example shows just the basic principle how to add an user-tag into a website using UHTML . It do not show the use of attribute functions or request initialisation, but including any of this two is trivial.
Example with all files and directories as needed by apache:
uHTML Example
(7.84kB, from 18.11.2016, 18:13:48)