Re: [Templates] Array of hashes or hash refs

[prev] [thread] [next] [Date index for 2004/09/24]

From: Harald Joerg
Subject: Re: [Templates] Array of hashes or hash refs
Date: 09:54 on 24 Sep 2004
Roderick A. Anderson asks:

> [...]
>    Currently I'm passing a hashref to the TT process
>
> 	$sut->process($filename, \%CSI); (No not Crime Scene 
> Investigation  :-)
>
> but now need to pass in a bunch of hashes(/refs) that will get displayed
> in a table.  So %CSI would have ToDay => 'Sep 23, 2004'; PageTitle => 'Not
> doing so well', etc.  I now need to pass in multiple hashes like 
> this.
>
> 	{ LastName => Smith, FirstName => John, ID => BR549 }

Note that you may omit quotes on hash keys, but not on values.

If you use strict.  Which is what you should do anyway ;-)

> What kind of structure do I need to construct and pass in to TT?  Heck .. 
> can I pass in multiple hashes or even arrays?

Just build a hash containing references to all your data structures:

    %CSI = (ToDay => 'Sep 23, 2004'; PageTitle => 'Not doing so well');
    @array = ([ LastName => 'Smith',    FirstName => 'John', ID => 'BR549' ]
             ,[ LastName => 'Anderson', FirstName => 'Rod',  ID => 'BR007' ]
             );

    $sut->process($filename
                 ,{'CSI'       => \%CSI
                  ,'array'     => \@array
                  }
                 );

...and in your template you can have things like:

At [% CSI.ToDay %] [% array.0.FirstName %] [% array.0.LastName %] was
   [% CSI.PageTitle %].
        -- 
        HTH,
haj


_______________________________________________
templates mailing list
templates@xxxxxxxxxxxxxxxx.xxx
http://lists.template-toolkit.org/mailman/listinfo/templates

[Templates] Array of hashes or hash refs
Roderick A. Anderson 23:39 on 23 Sep 2004

Re: [Templates] Array of hashes or hash refs
Axel Gerstmair 09:25 on 24 Sep 2004

Re: [Templates] Array of hashes or hash refs
Harald Joerg 09:54 on 24 Sep 2004

Re: [Templates] Array of hashes or hash refs
merlyn (Randal L. Schwartz) 12:09 on 24 Sep 2004

Re: [Templates] Array of hashes or hash refs
Harald Joerg 21:59 on 24 Sep 2004

Re: [Templates] Array of hashes or hash refs
Roderick A. Anderson 19:43 on 24 Sep 2004

Generated at 08:56 on 15 Mar 2005 by mariachi v0.52