[Templates] Fundamental problem with the "." operator and hash keys

Josh Rosenbaum josh@infogears.com
Wed, 12 Sep 2007 13:09:26 -0600


Mark Wagner wrote:
> In perl one accesses a value in a hash reference like so:
> 
> $a->{'foo'}.
> 
> For the Template Toolkit that would be written like so:
> 
> a.foo
> 
> However this seems to place the restriction that keys must follow the
> naming rules for variables (alphanumeric + _, leading char alpha).
> 
> I'm running into many situations where the key contains other characters
> such as " " or "-". What is the best way to handle these? I hope it isn't:
> 
> bar = "key with spaces-n-dashes"
> a.$bar

http://www.template-toolkit.org/pipermail/templates/2006-October/009062.html

Quoting Paul: "So - for your case [% foo.${"bar baz"} %] would work as well."

There's also the item vmethod:
http://www.template-toolkit.org/pipermail/templates/2006-October/009061.html
[% hash.item('foo') %]  # same as hash.foo

-- josh