adam | 3 Mar 16:54
Picon

metal:use-macro should be TALES?

Howdy.

I'm fairly new to petal, and have been battling through the standard problems (overcoming my aversion to &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

Jonathan Vanasco | 3 Mar 17:54

Re: metal:use-macro should be TALES?

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 | 6 Mar 15:15
Picon

metal:use-macro should be TALES?

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="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@...> 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

Peter Rabbitson | 19 Mar 18:35
Picon

Accessing coderefs

Greetings,

Is it a bug or a feature that if I have:

$petal->process ( { coderef => \&useful_sub } );

I can not do

...petal:content="coderef --var1 --var2"...

?

Thanks 

Bruno Postle | 20 Mar 12:21

Re: Accessing coderefs

Peter Rabbitson wrote:
> 
> Is it a bug or a feature that if I have:
> 
> $petal->process ( { coderef => \&useful_sub } );
> 
> I can not do
> 
> ...petal:content="coderef --var1 --var2"...

This would be ok as an object, what about it doesn't work?

--

-- 
Bruno

Peter Rabbitson | 20 Mar 15:09
Picon

Re: Accessing coderefs

On Mon, Mar 20, 2006 at 11:21:49AM +0000, Bruno Postle wrote:
> Peter Rabbitson wrote:
> >
> >Is it a bug or a feature that if I have:
> >
> >$petal->process ( { coderef => \&useful_sub } );
> >
> >I can not do
> >
> >...petal:content="coderef --var1 --var2"...
> 
> This would be ok as an object, what about it doesn't work?
> 
> -- 
> Bruno

Petal gives me no problems if I am to use object/method and supply
parameters. In my case it is very problematic to supply an object to
process(), so I was wondering if there is a way to call a coderef.

Peter

Bruno Postle | 23 Mar 18:25
X-Face

Re: Accessing coderefs

On Mon 20-Mar-2006 at 09:09 -0500, Peter Rabbitson wrote:
>
> Petal gives me no problems if I am to use object/method and supply
> parameters. In my case it is very problematic to supply an object to
> process(), so I was wondering if there is a way to call a coderef.

No, it seems that Petal explicitly stops you passing parameters to 
anything that isn't an object.

The following patch seems to make this work, though it hasn't had 
any thorough testing.  Can you try it out?

Index: lib/Petal/Hash/Var.pm
===================================================================
RCS file: /var/spool/cvs/Petal/lib/Petal/Hash/Var.pm,v
retrieving revision 1.17
diff -r1.17 Var.pm
98a99,100
>           unless (isa $current->{$next}, 'CODE')
>           {
100a103
>           }
130c133
<       $current = (isa ($current, 'CODE')) ? $current->() : $current;
---
>       $current = (isa ($current, 'CODE')) ? $current->(@args) : $current;

Jonathan Vanasco | 29 Mar 03:35

using $ in petal

i can't figure this out

if $a is in a document, petal shows me '$a' -- good.

if $aa is in a Petal template, its stripped out.  any $ followed by 2  
or more chars is stripped out.

does anyone know why?  bug , feature , workaround ?  i was trying to  
use petal to generate some downloadable customized php snippets, and  
i've tried just about every escape sequence with no luck.

Fergal Daly | 29 Mar 10:55
Picon

Re: using $ in petal

Petal is not strict TAL, it also includes some "features" like
$variable being the same as <span petal:replace="variable" />. So I
guess $aa is being replaced it's value (ie blank), my guess is that
something in the parser is wrong so it doesn't correctly recognise
single letter variables with $,

F

On 3/29/06, Jonathan Vanasco <jon@...> wrote:
> i can't figure this out
>
> if $a is in a document, petal shows me '$a' -- good.
>
> if $aa is in a Petal template, its stripped out.  any $ followed by 2
> or more chars is stripped out.
>
> does anyone know why?  bug , feature , workaround ?  i was trying to
> use petal to generate some downloadable customized php snippets, and
> i've tried just about every escape sequence with no luck.
>
>
>

Jonathan Vanasco | 29 Mar 18:37

Re: using $ in petal


On Mar 29, 2006, at 3:55 AM, Fergal Daly wrote:

> Petal is not strict TAL, it also includes some "features" like
> $variable being the same as <span petal:replace="variable" />. So I
> guess $aa is being replaced it's value (ie blank), my guess is that
> something in the parser is wrong so it doesn't correctly recognise
> single letter variables with $,

Are there any known workarounds?  I can't seem to figure out any way  
to put the string $abc into a document

\$var didn't work
$$var didn't work

$ isn't a standard html entity - its a 'special character' and not  
widely supported
&dollar; is on some spec sheets, but i haven't found a browser that  
shows it
&#036; is the equiv, but Petal seems to parse  it as a $ for  
interpolation ( so $a works, but $a[a]* doesn't )


Gmane