[Petal] repeat through hash

Corey corey_s at qwest.net
Fri Apr 21 00:24:13 BST 2006


On Thursday 20 April 2006 14:05, Jonathan Vanasco wrote:
>
> just be wary that its not strict tal - so you won't be able to use
> your Petal templates with python or php
>

That is an excellent point; I had taken pause on it as well, except
that I really was in a rush to get things working with what I had, so
I opted for it anyhow, hopefully as a temporary solution at least --
read on.


> generally, what i do is this:
>
> pull everything from the db into a hash using named keys , and a
> _HASHNAME_order array that has the order of the keys if necessary
> do what i must with the data for output, just create a new array,
> and loop through the order of hash keys tossing a ref to the element
> into the item
>

That's kindof what I was getting at, when I mentioned how I noticed
Petal seemed to make me design a bit of my code/libraries/modules
api and implementation around Petal, rather than just coding naturaly,
then using Petal to get at the data.

I think it would be beneficial to not require any consideration of one's
templating system whilst programming/designing one's software. i.e., it
shouldn't be necessary to have to create special accessors just to allow
your presentation/view templates to work in a convenient manner.

Take the following contrived example for an instance:

# get person by id

# innapropriate ( array ref )
$people = [
   {
      'firstname' => 'Benjamin',
      'lastname'  => 'Tucker'
      'id_num'    => '456a7'
   },
   {
      'firstname' => 'Pierre-Joseph',
      'lastname'  => 'Proudhon'
      'id_num'    => '765z4'
   }
];
# but Petal needs to loop through it, so we're resigned to modeling,
# or providing access to, our data in that way

# however, the following would make more sense....

# correct/natural ( hash ref )
$people = {

   '456a7' =>
      {
         'firstname' => 'Benjamin',
         'lastname'  => 'Tucker'
      },
   '765z4' =>
      {
         'firstname' => 'Pierre-Joseph',
         'lastname'  => 'Proudhon'
      }

};
# but Petal can't loop through hashes, so we're stuck jumping
# through hoops such as the former array ref, or such as what
# Jonathan described



With that in mind, I propose the following:

Allow 'repeat' to work seamlessly/transparently with hashes - in
array context 'repeat/index' carries the subscript, while in hash
context it would carry the key.

I'm thinking that such a solution would be fully compliant with the
TAL spec, while providing an extremely useful feature which would
help tremendously towards minimizing the existant
code_implementation-to-templating_sytem incongruency presented 
above.

Contrast the two following examples, notice how transparent it can
be made to work:

# current approach
$people = [
   {
      'firstname' => 'Benjamin',
      'lastname'  => 'Tucker'
      'id_num'    => '456a7'
   },
   {
      'firstname' => 'Pierre-Joseph',
      'lastname'  => 'Proudhon'
      'id_num'    => '765z4'
   }
];

<table>
   <tr petal:repeat="person people">
      <td petal:content="person/id_num"></td>
      <td petal:content="person/firstname"></td>
   </tr>
</table>

person/firstname  => 'Benjamin'
person/lastname   => 'Tucker'
person/id_num     => '765z4'
repeat/index      =>  0
repeat/number     =>  1


# if petal's 'repeat' supported hashes
$people = {
   '456a7' =>
      {
         'firstname' => 'Benjamin',
         'lastname'   => 'Tucker'
      },
   '765z4' =>
      {
         'firstname' => 'Pierre-Joseph',
         'lastname'   => 'Proudhon'
      }
};

<table>
   <tr petal:repeat="person people">
      <td petal:content="person/repeat/index"></td>
      <td petal:content="person/firstname"></td>
   </tr>
</table>

person/firstname  => 'Benjamin'
person/lastname   => 'Tucker'
repeat/index      => '765z4'
repeat/number     =>  1


And for a more flat, simple example:

$people = [ 'Benjamin Tucker', 'Pierre-Joseph Proudhon' ];

# versus

$people = {

   '456a7' => 'Benjamin Tucker',
   '765z4' => 'Pierre-Joseph Proudhon'

};

# would result in

<table>
   <tr petal:repeat="person people">
      <td petal:content="person"></td>
   </tr>
</table>

person        => 'Benjamin Tucker'
repeat/index  =>  0 or '456a7'


Anyone in favor? Any pitholes or details I'm missing?


Cheers,

Corey


More information about the Petal mailing list