I put together a filter function that should convert dreamweaver templates into HTML::Templates. I'm not sure how well it would convert into HTML::Template::Expr, but it gives other people a decent starting point. BTW, if you haven't checked out the dreamweaver mx software, I'd highly recommend it - there's a free 30 day trial period.

package HTML::Template::DWT;

require Exporter;
@ISA = qw( Exporter );
@EXPORT_OK = qw( DWT2HTML );

use strict;
use warnings;

sub escapeQuote {
my $toencode = shift;

if ( $toencode =~ /\'/ ) {
$toencode =~ s{\"}{\'}gso;
return "\"$toencode\"";
}
else {
return "'$toencode'";
}
}

sub handleTemplateExpr {
my $str = shift;
my $defaults = shift;

my $ret = "<!-- TMPL_VAR";
my $name;
if ( $str =~ m{\s[Ee][Xx][Pp][Rr]=([\"\'])(.*?)\1}s ) {
$name = $2;
$ret .= " NAME=$1$2$1";
}

if ( $str =~ m{\s[Ee][Ss][Cc][Aa][Pp][Ee]=([\"\'])(.*?)\1}s ) {
$ret .= " ESCAPE=$1$2$1";
}


if ( $defaults->{ $name } ) {
if ( $defaults->{ $name } =~ /\"/ ) {
$ret .= " DEFAULT='$defaults->{ $name }'";
}
else {
$ret .= " DEFAULT=\"$defaults->{ $name }\"";
}
}

$ret .= " -->";
return $ret;
}

sub DWT2HTML {
my $dwt = shift;

my %params;
while ( $$dwt =~ /<!--\s*[Tt][Ee][Mm][Pp][Ll][Aa][Tt][Ee][Pp][Aa][Rr][Aa][Mm](.*?)-->/g ) {
my $text = $1;

my $name;
if ( $text =~ m{\s[Nn][Aa][Mm][Ee]=([\'\"])(.*?)\1} ) {
$name = $2;
}
else {
next;
}

my $val;
if ( $text =~ m{\s[Vv][Aa][Ll][Uu][Ee]=([\'\"])(.*?)\1} ) {
$val = $2;
}
else {
next;
}

my $type = "";
if ( $text =~ m{\s[Tt][Yy][Pp][Ee]=([\'\"])(.*?)\1} ) {
$type = $2;
}

if ( $type eq "boolean" ) {
$params{ $name } = $val eq "false" ? 0 : 1;
}
else {
$params{ $name } = $val || "";
}
}

$$dwt =~ s/_document\[([\'\"])(.*?)\1\]/$2/sg;
$$dwt =~ s/_repeat\[([\'\"])(.*?)\1\]/$2/sg;
$$dwt =~ s/<!--\s*[Tt][Ee][Mm][Pp][Ll][Aa][Tt][Ee][Pp][Aa][Rr][Aa][Mm].*?-->\s*//sg;

$$dwt =~ s{<!--\s*[Tt][Ee][Mm][Pp][Ll][Aa][Tt][Ee][Bb][Ee][Gg][Ii][Nn][Ee][Dd][Ii][Tt][Aa][Bb][Ll][Ee]\s+[Nn][Aa][Mm][Ee]=([\"\'])(.*?)\1\s*-->(.*?)<!--\s*[Tt][Ee][Mm][Pp][Ll][Aa][Tt][Ee][Ee][Nn][Dd][Ee][Dd][Ii][Tt][Aa][Bb][Ll][Ee]\s*-->}{<!-- TMPL_VAR NAME=$1$2$1 -->}sg;

$$dwt =~ s{\@\@\((.*?)\)\@\@}{sprintf( "<!-- TMPL_VAR NAME='%s' ESCAPE='HTML'%s -->", $1, $params{$2} ? " DEFAULT=" . escapeQuote( $params{$2} ) : "" ) }esg;

$$dwt =~ s{<!-- TemplateExpr(.*?)-->}{handleTemplateExpr($1, \%params)}esg;

$$dwt =~ s{<!--\s*[Tt][Ee][Mm][Pp][Ll][Aa][Tt][Ee][Bb][Ee][Gg][Ii][Nn][Ii][Ff]\s+[Cc][Oo][Nn][Dd]=([\"\'])(.*?)!=([\'\"])\3\1\s*-->}{<TMPL_IF NAME=$1$2$1>}sg;
$$dwt =~ s{<!--\s*[Tt][Ee][Mm][Pp][Ll][Aa][Tt][Ee][Bb][Ee][Gg][Ii][Nn][Ii][Ff]\s+[Cc][Oo][Nn][Dd]=([\"\'])!(.*?)\1\s*-->}{<TMPL_IF NAME=$1$2$1><TMPL_ELSE>}sg;
$$dwt =~ s{<!--\s*[Tt][Ee][Mm][Pp][Ll][Aa][Tt][Ee][Bb][Ee][Gg][Ii][Nn][Ii][Ff]\s+[Cc][Oo][Nn][Dd]=([\"\'])(.*?)\1\s*-->}{<TMPL_IF NAME=$1$2$1>}sg;
$$dwt =~ s{<!--\s*[Tt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