HTML-DWT
view release on metacpan or search on metacpan
package HTML::DWT;
#############################################################
# Version 2.08
#
# Copyright (c) 2002 by S.D. Campbell <whytwolf@spots.ab.ca>
#
# Created 03 March 2000; Revised 04 March 2002 by SDC
#
# A perl module designed to parse a simple HTML template file
# generated by Macromedia Dreamweaver and replace fields in the
# template with values from a CGI script.
#
#############################################################
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#############################################################
use Exporter;
use Carp;
use File::Find;
use File::Basename;
use XML::Simple;
@ISA = qw(Exporter);
@EXPORT = qw(fillTemplate fill export);
@EXPORT_OK = qw(output param query clear_params);
%EXPORT_TAGS = (
Template => [qw(output param query clear_params)],
);
use strict;
use vars qw($errmsg $VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS
$NOTICE %DWT_FIELDS %DWT_VALUES $fname $filepath @search);
$VERSION = '2.08';
$NOTICE = "\n<!-- Generated using HTML::DWT version " . $VERSION . " -->\t\t\n";
$NOTICE .= "<!-- HTML::DWT Copyright (c) 2001,2002 Sean Campbell -->\t\n";
$NOTICE .= "<!-- HTML::DWT is licenced under the GNU General Public License -->\t\n";
$NOTICE .= "<!-- You can find HTML::DWT at http://www.spots.ab.ca/~whytwolf -->\t\n";
$NOTICE .= "<!-- or by going to http://www.cpan.org -->\n";
%DWT_FIELDS = ();
%DWT_VALUES = ();
$errmsg = "";
$fname = '';
$filepath = '';
@search = ();
#############################################################
# new
#
# The constructor for the class. Requires a HTML Template
# filename. Returns a reference to the new object or undef
# on error. Errors can be retrieved from $HTML:DWT:errmsg.
sub new {
my $class = shift;
my $xs = new XML::Simple(keeproot => 1);
my $ref = $xs->XMLin($$self{xml});
my $items = $ref->{templateItems}->{item};
foreach my $item (keys %$items){
$self->param($item => $ref->{templateItems}->{item}->{$item}->{content});
}
}
}
1;
__END__
=head1 NAME
HTML::DWT - DreamWeaver HTML Template Module
=head1 INSTALLATION
=head2 Unzip/tar the archive:
tar xvfz HTML-DWT-2.08
=head2 Create the makefile
perl Makefile.PL
=head2 Make the module (must have root access to install)
make
make test
make install
=head1 SYNOPSIS
use HTML::DWT;
$template = new HTML::DWT(filename => "file.dwt");
%dataHash = (
doctitle => 'DWT Generated',
leftcont => 'some HTML content here'
);
$html = $template->fill(\%dataHash);
or
use HTML::DWT qw(:Template);
$template = new HTML::DWT(filename => "file.dwt");
$template->param(
doctitle => '<title>DWT Generated</title>',
leftcont => 'Some HTML content here'
);
$html = $template->output();
=head1 DESCRIPTION
A perl module designed to parse a simple HTML template file
generated by Macromedia Dreamweaver and replace fields in the
template with values from a CGI script.
=head1 METHODS
=head2 Options
use HTML::DWT qw(:Template);
Using the Template option allows for built in support in HTML::DWT
for the HTML::Template invocation syntax (param(), output() etc.) See
HTML::Template for more details. It is best to require a version of
2.05 for HTML::DWT to support this option.
=head2 new()
new HTML::DWT("file.dwt");
new HTML::DWT(
filename => "file.dwt",
associate => $q,
case_sensitive => 1,
no_includes => 1,
path => '/var/www/html',
xml => $xml-data,
);
Creates and returns a new HTML::DWT object based on the Dreamweaver
template 'file.dwt' (can specify a relative or absolute path). The
Second instance is recommended, although the first style is still
supported for backwards compatability with versions before 2.05.
B<associate>:
The associate option allows the template to inherit parameter
values from other objects. The object associated with the template
must have a param() method which works like HTML::DWT's param().
Both CGI and HTML::Template fit this profile. To associate another
object, create it and pass the reference scalar to HTML::DWT's new()
method under the associate option (see above).
B<case_sensitive>:
The case_sensitive option allows HTML::DWT to treat template fields
in a case-sensitive manner. HTML::DWT's default behavior is to match
all fields in a case-insensitive manner (i.e. doctitle is considered
the same as DOCTITLE or DocTitle). Set case_sensitive to 1 to over-
ride this default behavior.
B<no_includes>:
HTML::DWT will by default look for any included Dreamweaver library
item files (.lbi files) that may be specified in the template using
the <!-- #BeginLibraryItem "file.lbi" -> field. The module will open
the specified library file and will include the file's contents in
the generated HTML. Setting no_includes to 1 will over-ride this
default behavior.
B<path>:
HTML::DWT will accept an array of paths under which it will look for
template and library files. The module will also look in directories
specified by the environment variables $HTML_TEMPLATE_ROOT and
$DOCUMENT_ROOT. Absolute path names are not checked, although
the pseudo-paths '/Library/' and '/Templates/' are treated as
relative paths.
B<xml>:
HTML::DWT will accept a string value containing an XML document that
conforms to the HTML-DWT DTD. This string may be associated with
the template object through the XML option in the constructor. Each
<item> tag in the XML document will have its contents loaded into
a corresponding template field. A valid HTML-DWT XML document will
look like this:
<?xml version="1.0"?>
<templateItems template="/Templates/temp.dwt">
<item name="centercont"><![CDATA[Testing]]></item>
<item name="doctitle"><![CDATA[<title>testing</title>]]></item>
<item name="leftcont"><![CDATA[Testing]]></item>
<item name="rightcont"><![CDATA[Testing]]></item>
</templateItems>
These documents can be automaticly generated both by HTML::DWT based
on template data using export(), or by Macromedia Dreamweaver.
=head2 fill()
$template->fill(\%dataHash);
$template->fillTemplate(\%dataHash);
Takes a hash reference where the keys are the named areas of the
template and the associated values are HTML content for those
areas. This method returns a complete HTML document, which can
then be sent to STDOUT (the browser). The fill() method is the
prefered means of accessing this functionality; fillTemplate()
is implemented only to support versions of HTML::DWT earlier than
version 2.05.
=head2 param()
$template->param();
$template->param('doctitle');
$template->param(
doctitle => '<title>DWT Generated</title>',
leftcont => 'Some HTML content here'
);
Takes a hash of one or more key/value pairs, where each key is a named
area of the template, and the associated value is the HTML content for
that area. This method returns void (HTML substitiutions are stored
within the object awaiting output()).
If called with a single paramter--this parameter must be a valid field
name--param() returns the value currently set for the field, or undef
if no value has been set.
If called with no parameters, param() returns a list of all field names.
NOTE: All Dreamweaver templates store the HTML page's title in a field
named 'doctitle'. HTML::DWT will accept a raw title (without <title>
tags) and will add the appropriate tags if the content of the 'doctitle'
field should require them.
This is a HTML::Template compatible method.
=head2 clear_params()
$template->clear_params();
Clears all field values from the template's parameter list and sets each
parameter to an undefined value.
This is a HTML::Template compatible method.
=head2 output()
$template->output();
$template->output(print_to => \*STDOUT);
Returns the parsed template and its substituted HTML for output.
( run in 0.498 second using v1.01-cache-2.11-cpan-e1769b4cff6 )