为什么perl数组值没有使用html模板在下拉列表中列出?

时间:2016-01-10 02:39:10

标签: perl

我的perl cgi页面下拉菜单没有列出值。我的cgi脚本

use HTML::Template;use CGI;
use CGI::Carp qw(fatalsToBrowser);
my $template = HTML::Template->new(filename => 'test.tmpl');
@count = ("abc,xyz,123,test");
print $cgi->start_form(
                        -name => 'mainpage',
                        -method => 'POST',
                        );


$template->param( COUNT => \@count);
print $template->output();

my test.tmpl

<div class="row ">
            <div class="col-lg-8 col-lg-offset-2">
                                <div class="dropdown">
                                <div class="input-field col s6">
                                <input name="count" id="countid" type="text" class="validate">
                                <label for="count">count</label>
    <select>
        <TMPL_LOOP NAME="COUNT">
        <option value="<TMPL_VAR NAME=VALUE>"><TMPL_VAR NAME=NAME></option>
        </TMPL_LOOP>
    </select>                                
</div>

in the drop down menu, I am expecting below values. Please suggest if I need to make any change on above code on cgi or html template
abc
xyz
123
test

请找到修改后的cgi脚本值。 cgi page doesn; t在下拉选项卡中显示值。但是在html页面中呈现了这些值

$count = [
{ name => 'count1', value => 1 },
{ name => 'count2', value => 2 },
{ name => 'count3', value => 3 },
];

   $template->param(COUNT => [{name => 'count1', value => 1}, {name =>  'count2', value => 2}, {name => 'count3', value => 3}]);

print $template->output, "\n";

下面呈现的html页面
<div class="row ">
            <div class="col-lg-8 col-lg-offset-2">
                <div class="dropdown">
            <div class="input-field col s6">
                <input name="count" id="countstate" type="text" class="validate">
                            <label for="count">count</label>
<select>

<option value="1">count1</option>

<option value="2">count2</option>

<option value="3">count3</option>

</select>                                

1 个答案:

答案 0 :(得分:1)

您的数组包含单个元素:字符串abc,xyz,123,test。如果您希望每个事物都是独立的元素,则需要以不同的方式创建数组:

@count = ('abc', 'xyz', 123, 'test');
@count = qw(abc xyz 123 test);

更新:好的,在阅读<TMPL_LOOP>的文档后,看起来您确实需要arrayref of hashrefs

$count = [
    { name => '???', value => 'abc' },
    { name => '???', value => 'xyz' },
    { name => '???', value => '123' },
    { name => '???', value => 'test' },
];

$template->param(COUNT => $count);
$template->param(COUNT => [{...}, {...}, {...}, ...]);  # same thing

但是,我无法从你的问题中看出哪一个应该是标签,哪一个应该是价值,所以我将把它作为练习让你完成。