Perl模板工具包<%IF%>条件似乎不起作用

问题描述

当哈希值'Season'具有值时,我想显示下面的html部分。麻烦的是,即使Season没有值,我什至都没有向模板发送名为Season的变量,或者即使我将下面的行更改为<% IF XSeason.size %>

<% IF Season.size %> <!-- season data -------------------------------------->
<div class="container" style="margin-top:20px;">
  <div class="row">
    <h3 class="text-center"> This should only show up if Season hash has value</h3>
    <div class="col-md-12">
      <div class="col-md-10">
    <div class="form-group">
      <table style="width:100%; line-height:40px;">   <!-- Feats (so far) as a table // -->
        <% FOREACH $name in Seasonkeys %>
        <tr>
          <form role="form" action="../alpview" method="POST">
        <td width="5"><% Season$name.stat %></td> 
        <td width="5"><% Season$name.AVG %></td> 
        <input type="hidden" name="chlng_id" value="<% Season$ID.ID %>" />
          </form>
        </tr>   
        <% END %>   
      </table>
    </div>
      </div>
    </div>
  </div>
</div>
<% END %>

更新:这是一个简单的示例。

<h2>Lets start with an array reference<h2>
<h3>What about fruits array: </h3>
<% FOREACH fruit IN fruits %>
<% fruit %>
<% END %>
<p>array size: </p><% fruits.size %></p>
<p>array dumped: </p><% USE Dumper; Dumper.dump(fruits) %>

输出:

Lets start with an array reference
What about fruits array:
apple bananna orange
array size:

array dumped:

解决方法

您的主张很容易被驳回:

use 5.014;
use warnings;

use Template qw( );

my $template = '<% IF Season.size %>Test<% END %>';

my $tt = Template->new({
   START_TAG => '<%',END_TAG   => '%>',});

print "No var:         ";
$tt->process(\$template,{ })
   or warn($tt->error());
say "";

print "Empty hash:     ";
$tt->process(\$template,{ Season => { } })
   or warn($tt->error());
say "";

print "Not empty hash: ";
$tt->process(\$template,{ Season => { a => 4 } })
   or warn($tt->error());
say "";

输出:

No var:
Empty hash:
Not empty hash: Test

您的哈希不能为空。

,

尝试将其添加到<% IF ... %>标签之后。

<% Season.size %>
<% USE Dumper; Dumper.dump(Season) %>

至少您会看到TT认为该变量是什么。

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...