For what it's worth, I'm entirely in agreement with you about HTML templating.
Any sufficiently mature templating language starts to exhibit signs of following Greenspun's tenth rule. It acquires bits of syntax for conditionals, looping and so on. They become Turing complete. And if I want to generate my HTML using a Turing-complete language, I've already got one right in front of me: Perl.
I do use templates occasionally. And when I do, I tend to use Text::Template which allows me to embed Perl in the templates thank-you-very-much and not some poorly designed ersatz scripting language.
But that's only occasionally; and not usually for outputting HTML, but for outputting other formats. Why? Because of the other thing I dislike about templating...
The other thing I dislike about templating is that it composes HTML using a linear, string-like paradigm, whereas an HTML document is really a tree-like structure that just happens to have been serialized down to a string.
If I have a template like:
<div style="color:red">[% ... %]</div>
Then I have no confidence that the end </div> tag will end up closing the initial start <div> tag. The stuff within the [% ... %] may well do something like </div><div>. It's simply a flaky way to compose a tree. If I do this:
div( ... );
... and div() is written to return a DOM node, and all the parameters passed to it are DOM nodes, then I know my resulting document is going to be sane.
My own attempt to write something along the lines of what Lady_Aleena has done above, is HTML::HTML5::Builder.
Basic usage is along these lines...
use HTML::HTML5::Builder qw[:standard];
my @things = (
"raindrops on roses",
"whiskers on kittens",
"bright copper kettles",
"warm woollen mittens",
);
print html(
-lang => 'en',
head(
title('Test'),
Meta(-charset => 'utf-8'),
),
body(
h1('Test'),
p('Here are a few of my favourite things...'),
ul(map li($_), @things),
),
);
The module really needs a revisit. The problem is that many HTML elements have names that conflict with Perl's built-ins (tr, map, etc). Right now, HTML::HTML5::Builder exports some of these as capitalized functions to deal with that issue, but I'm not especially happy with the current solution.
package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
|