Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding values to a hash in Template Toolkit

Tags:

hash

perl

I have a hash whose keys I am iterating over in Template Toolkit. The example is below....

<SELECT NAME="SelectList_[% feed.num %]" ID="SelectList_[% feed.num %]" SIZE="5" style="width: 250px;" MULTIPLE>
  [% FOREACH xvar = feed.xvars.keys %]
     <OPTION VALUE="[% xvar %]">[% xvar %]</OPTION>
  [% END %]
     <OPTION VALUE="X_File_Name">X_File_Name</OPTION>
</SELECT>

What I need to do is to alphabetize this SELECT list (using sort, which I know how to do. Problem is that

<OPTION VALUE="X_File_Name">X_File_Name</OPTION>

line. I was hoping to just add that value "X_File_Name" to the feed.xvars hash. Something like this...

[% feed.xvars = { "X_File_Name" => "1" } %] 

hoping that that would add the value to the hash (as opposed to obliterating it). No such luck. Looking in the Template Toolkit book and googling doesn't yield anything either. Anyone know how to do this?

like image 755
Jane Wilkie Avatar asked Feb 17 '11 19:02

Jane Wilkie


2 Answers

After I asked this I figured it out.

[% appendval = { "X_File_Name" => "1" } %]
[% feed.xvars.import(appendval) %]
like image 127
Jane Wilkie Avatar answered Oct 14 '22 21:10

Jane Wilkie


There's a far easier approach:

[% feed.xvars.X_File_name = 1 %]

You access individual elements in a TT hash using the dot syntax.

like image 2
Dave Cross Avatar answered Oct 14 '22 23:10

Dave Cross