【问题标题】:template toolkit - Iterating through array of hashes模板工具包 - 遍历哈希数组
【发布时间】:2012-09-06 12:43:36
【问题描述】:

我在网上找不到这个问题的答案。

使用 Dancer 和 Template 工具包,我有一个哈希数组,我想将其打印到我的 web 应用程序的表格中。 这就是我将数据结构发送到模板的方式:

template "find_ip_results2", {
    user => \@user,
};

迭代此结构并访问每个键值对的正确 foreach 语法是什么?

【问题讨论】:

  • @user 看起来像什么?元素是hashrefs吗?你展示它的方式,这是一个数组的哈希,而不是一个哈希数组。
  • 数组看起来像这样: $VAR1 = { 'communities' => 'bar', 'priority' => ' 3', 'policyName' => 'TATA', }; $VAR2 = { 'communities' => 'baz', 'priority' => '1', 'policyName' => 'Google', }; $VAR3 = { 'communities' => 'foo', 'priority' => '4', 'policyName' => 'Peering', };

标签: perl iteration template-toolkit dancer


【解决方案1】:
[% FOR u = user %]
    [% u.key    # or u.item('key') %]
[% END %]

您可以阅读文档here

【讨论】:

    【解决方案2】:

    对 eugene y 修复迭代器语法的答案稍作修改:

     <ul>
     [% FOREACH u IN user %]
        <li>
          communities: [% u.communities %]<br/>
          priority: [% u.priority %]<br/>
          policyName: [% u.policyName %]
        </li>
     [% END %]
     </ul>
    

    【讨论】:

      猜你喜欢
      • 2014-09-26
      • 2016-09-03
      • 2022-01-19
      • 1970-01-01
      • 1970-01-01
      • 2012-08-22
      • 1970-01-01
      • 2015-08-26
      相关资源
      最近更新 更多