NAME
DSL::HTML - Declarative DSL(domain specific language) for writing HTML
templates within perl.
DESCRIPTION
Templating systems suck. This sucks less.
In most cases a templating system lets you write html files with
embedded logic. The embedded logic can be a template specific language,
or it can let you embed code from the projects programming language.
An alternative that has been played with is constructing the HTML
directly in your application language. In most cases this sucks more.
OOP, where objects are built and then associated via method calls is NOT
a friendly way to build a complex tree.
DSL::HTML takes the alternative approach, but does it in a significantly
more elegent way. Instead of forcing you to construct objects and build
a tree manually via methods, you define the tree via nested subroutines.
This is sort of a functional approach to tree building.
EARLY VERSION WARNING
THIS IS AN EARLY VERSION! Basically I have not decided 100% that the API
will remain as-is (though it likely will not change much). I am also
embarrased to admit that this code is very poorly tested (Yes, this is
more embarrasing considering I wrote Fennec).
BENEFITS
The template language is perl
There is no embedded template language, your logic is all in perl,
you treat all tags like perl objects. The nested block syntax allows
this while also saving you from the PITA of direct object
manipulation; that is you do not need to say
"$tag->insert(Tag->new)" or similar.
No need to hand-write html
Hand-written HTML is easy to screw up. You might forget to close a
tag, or nest things improperly. The nature of browsers it to try to
make it work anyway, so you can sometimes spend hours debugging
broken html.
Syntax checking is done by perl
The nested-blok syntax is checked by perl. If you make a syntax
error, or a typo, perl will typically catch your mistake when you
try to build the package.
Templates and tags are built like subroutines
This means you call your template with argumnets similar to how you
can any function with arguments.
Templates can be imported/exported between modules.
You can create perl modules that are simply template libraries,
other modules can load these libraries to gain access to the
templates.
SYNOPSYS
# Note: This brings in an import() method.
# See the EXPORTS - import() section later in this doc for more info.
use DSL::HTML;
template my_template {
my @options = @_;
# The lexical variable '$tag' is defined for you automatically and is a
# reference to the current tag on the stack (usually
)
$tag->attr( foo => 'bar' );
css 'my/css/file.css'; # Goes to the header
tag h1 { "Welcome!" }
tag h2 { "Choices:" }
# Tags nest naturally, here is a
TEMPLATE OBJECT
If you do not like defining templates as package meta-data you can use
them in a less-meta form:
use strict;
use warnings;
use DSL::HTML;
my $ulist = template ulist {
# DSL::HTML::Rendering.
my @items = @_;
css 'ulist.css';
tag ul(class => 'my_ulist') {
for my $item (@items) {
tag li { $item }
}
}
}
my $list_pair = template list_pair {
my ($items_a, $items_b) = @_;
$ulist->include( @$items_a ); # Using the ulist template above
$ulist->include( @$items_b ); # " "
# Alternatively you could do:
# include $ulist => ...;
# the 'include' keyword works with at emplate object as an argument
}
my $html = $list_pair->compile(
[qw/red green blue/],
[qw/one two three/],
);
# You could also do:
# build_template $list_pair => (...);
print $html;
Should give us:
red
green
blue
one
two
three
SEE ALSO
HTML::Declare
HTML::Declare seems to be a similar idea, but I dislike the feel of
it. That said still have to give the author props for doing it as
good as possible without Devel::Declare.
AUTHORS
Chad Granum exodist7@gmail.com
COPYRIGHT
Copyright (C) 2013 Chad Granum
DSL-HTML is free software; Standard perl license (GPL and Artistic).
DSL-HTML 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 license for more details.