[Petal] metal:use-macro should be TALES?

adam adamlounds at gmail.com
Mon Mar 6 14:15:25 GMT 2006


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 &s with & - I'm expecting something like

<a href="http://server/cgi?foo=bar&baz=boogie">http://server/cgi?foo=bar&amp;baz=boogie</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 = $petal_object->_file();
     $included_from =~ s/\#.*$//;

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

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

     (defined $lang and $lang) ?
-        $class->add_code ("my \$res = eval { Petal->new (file =>
'$path', lang => '$lang')->process (\$new_hash) };") :
-       $class->add_code ("my \$res = eval { Petal->new
('$path')->process (\$new_hash) };");
+        $class->add_code ("my \$res = eval { Petal->new (file =>
\$path, lang => '$lang')->process (\$new_hash) };") :
+       $class->add_code ("my \$res = eval { Petal->new
(\$path)->process (\$new_hash) };");
·····
     $class->add_code ("\$res = \"<!--\\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="${dat/header}" />
 would be canonicalized to
<?include href="<?var name="dat/header"?>"?>
or whatever and expanded accordingly. I can see how nested expressions
would be complicated though :-)

On 3/3/06, Jonathan Vanasco <jon at 2xlp.com> wrote:
> Usually for things like that, i do:
>
> <tal tal:omit-tag="string:1" tal:define="dynVar foo/${dat/bar}/
> baz.xml"/>
> <span tal:attributes="class var"/>
>
> I'm not sure if that will work in your example.
>
> btw, you can just
>         $processed =~ 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="dat/header" or
> > metal:use-macro="foo/${dat/bar}/baz.xml" or
> > metal:use-macro="string: dat/header"
> >
> > Is this supported? Should I be doing something entirely different?
> >
> > Thanks for your help,
> > --
> > Adam
--
Adam


--
Adam


More information about the Petal mailing list