[Petal] Looping through a hash

Jean-Michel Hiver jhiver at mkdoc.com
Fri Sep 5 13:08:10 BST 2003


> > 	<span petal:repeat="key keys: $some_hash">
> > 		do something with $key ...
> > 	    will this get the value: ${some_hash/$key} ?
> > 	</span>
> 
> Nice try but Petal isn't getting the value. I tried various
> modifications but Petal doesn't want to allow the dynamic key. Could one
> of the Petal engine experts comment on this?

Petal doesn't support that kind of recursive stuff. Fergal's CodePerl
might do.

So you need to write not only a hash_keys: modifier, but also a
hash_value: modifier. I've attached an example.


> While we are on the topic of the Petal engine, I'm trying to pass a
> string value in as a parameter to one of my modifiers. Is this allowed?
> What I'm trying to do is allow an arbitrary string to be the date
> separator. So my proposed modifier would be called as follows:
> 	<p tal:content="us_date: $date -">date</p>

This is fine. You just have to realize that your modifier will deal with
a string ' $date -'. It's up to your modifier to parse this string to
separate 'date' and '-'.

I suggest you take a look at how the 'string:' modifier is working and
adapt it to your case.


> > I've got a few little plugins like this that might be good candidates 
> > for a Petal::Utils package at some stage.
> 
> Building a utils package has been on my to do list for several months
> now, so I used this opportunity to actually build it and combine several
> of my modifiers. Since you had the idea first, would you like to create
> it and put it onto CPAN?
> 
> I have attached a tarfile of my initial version since it's less than 4K.
> Comments appreciated. The broken tests have been commented out in the
> 01.t file.

It would be cool to see some Petal contributions on CPAN -
Great stuff :)


Cheers,
-- 
Building a better web - http://www.mkdoc.com/
---------------------------------------------
Jean-Michel Hiver
jhiver at mkdoc.com  - +44 (0)114 255 8097
Homepage: http://www.webmatrix.net/


More information about the Petal mailing list