Jane Wilkie
Jane Wilkie

Reputation: 1743

Adding values to a hash in Template Toolkit

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?

Upvotes: 6

Views: 2187

Answers (2)

Dave Cross
Dave Cross

Reputation: 69264

There's a far easier approach:

[% feed.xvars.X_File_name = 1 %]

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

Upvotes: 2

Jane Wilkie
Jane Wilkie

Reputation: 1743

After I asked this I figured it out.

[% appendval = { "X_File_Name" => "1" } %]
[% feed.xvars.import(appendval) %]

Upvotes: 4

Related Questions