如何处理模板工具包中包含非法标识符字符的哈希键? [英] How can I handle hash keys containing illegal identifier characters in Template Toolkit?

查看:84
本文介绍了如何处理模板工具包中包含非法标识符字符的哈希键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在模板工具包中,如果我有以下包含hashref的变量:

In Template Toolkit, if I have the following variable containing a hashref:

[% 
   artist = {
     'life-span' => '1975 to 1987',
   }
%]

以寿命"方式输出数据的最佳方法是什么?

What is the best way to output the data in 'life-span'?

我尝试过...

[% artist.life-span %]

^由于连字符,此操作失败.

^This fails because of the hyphen.

[% artist.'life-span' %]

^此操作失败,因为语法不正确.

^This fails because the syntax is incorrect.

[% lifespan = 'life-span' %]
[% artist.$lifespan %]

^这可行,但在具有大量数据的大型应用程序中不可行.

^This works, but is impractical in a large app with lots of data.

有更好的方法吗?

我正在处理的项目是基于Catalyst的Web应用程序,数据来自许多不同的外部Web服务,因此我无法控制哈希键的名称.我想我可以在Perl中预处理数据,在将问题哈希键提供给TT之前重命名问题哈希键,但这似乎是一个很糟糕的解决方法.

The project I'm working on is a Catalyst based web app and the data comes from a number of different external web services, so I do not control the name of the hash keys. I guess I could pre-process the data in Perl, renaming the problem hash keys before giving it to TT, but that seems like a pretty crappy work-around.

推荐答案

您可以使用 查看全文

登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆