metal:use-macro should be TALES?

adam <[email protected]> Mon, 6 Mar 2006 14:15:25 +0000
Newsgroups gmane.comp.lang.perl.modules.petal
Message-ID <[email protected]>
Not quite, I want to choose the file to include, not just have a
string " foo/${dat/bar}/
baz.xml" in the output.

I'm very wary of replacing all &amp;s with & - I'm expecting something like

<a href=3D"http://server/cgi?foo=3Dbar&baz=3Dboogie">http://server/cgi?foo=
=3Dbar&amp;baz=3Dboogie</a>

where the visible content has &amp; but the href has &, so replacing
all &amp;s would be Bad.

Patch:
--- lib/Petal/CodeGenerator.pm  2006-01-17 12:27:53.000000000 +0000
+++ libnew/Petal/CodeGenerator.pm       2006-03-06 11:40:46.000000000 +0000
@@ -233,15 +233,23 @@
     my $included_from =3D $petal_object->_file();
     $included_from =3D~ s/\#.*$//;

+    # allow ${dat/var} expansion in includes
+    if($path=3D~ /^\$\{(.*)\}$/ ) {
+       $path =3D $class->comp_expr_encoded ($1);
+    } else {
+       $path =3D "'$path'";
+    }
+
     $class->add_code ("do {");
     $class->indent_increment();

     $class->add_code ("my \$new_hash =3D \$hash->new();");
     $class->add_code ("\$new_hash->{__included_from__} =3D '$included_from=
';");
+    $class->add_code ("my \$path =3D $path;");

     (defined $lang and $lang) ?
-        $class->add_code ("my \$res =3D eval { Petal->new (file =3D>
'$path', lang =3D> '$lang')->process (\$new_hash) };") :
-       $class->add_code ("my \$res =3D eval { Petal->new
('$path')->process (\$new_hash) };");
+        $class->add_code ("my \$res =3D eval { Petal->new (file =3D>
\$path, lang =3D> '$lang')->process (\$new_hash) };") :
+       $class->add_code ("my \$res =3D eval { Petal->new
(\$path)->process (\$new_hash) };");
=B7=B7=B7=B7=B7
     $class->add_code ("\$res =3D \"<!--\\n\$\@\\n-->\" if (defined \$\@
and \$\@);");
     $class->add_code ("\$res;");

A bit (OK, a lot) hacky, but it allows me to dynamically choose the
file to be included.

I think the underlying problem is that nested variables aren't
supported, as otherwise
<xi:include href=3D"${dat/header}" />
 would be canonicalized to
<?include href=3D"<?var name=3D"dat/header"?>"?>
or whatever and expanded accordingly. I can see how nested expressions
would be complicated though :-)

On 3/3/06, Jonathan Vanasco <[email protected]> wrote:
> Usually for things like that, i do:
>
> <tal tal:omit-tag=3D"string:1" tal:define=3D"dynVar foo/${dat/bar}/
> baz.xml"/>
> <span tal:attributes=3D"class var"/>
>
> I'm not sure if that will work in your example.
>
> btw, you can just
>         $processed =3D~ s/&amp;/&/g ;
>
> i do that for a bunch of petal symbols when doing emails/etc
>
>
> On Mar 3, 2006, at 10:54 AM, adam wrote:
>
> > Howdy.
> >
> > I'm fairly new to petal, and have been battling through the
> > standard problems (overcoming my aversion to &amp;s in hrefs,
> > javascript stuff etc etc), but am stumped on the latest one.
> >
> > Basically I want to choose which metal template to use at run-time,
> > using something along the lines of
> >
> > metal:use-macro=3D"dat/header" or
> > metal:use-macro=3D"foo/${dat/bar}/baz.xml" or
> > metal:use-macro=3D"string: dat/header"
> >
> > Is this supported? Should I be doing something entirely different?
> >
> > Thanks for your help,
> > --
> > Adam
--
Adam


--
Adam