【问题标题】:Unable to return the perl Hash into the template toolkit file无法将 perl 哈希返回到模板工具包文件中
【发布时间】:2013-04-20 15:08:27
【问题描述】:

提前致谢。 我正在尝试使用模板工具包文件显示文件列表。在此之前,我将 perl 文件中的哈希返回到 .tt(模板工具包文件)文件中。 但是条件没有执行,我也无法显示文件列表。 我提供文件供您参考。

Perl 文件(Example.pm):

sub example{ 
    my $path = "/sa2/tools/jayaram_delete";
    if (chdir($path)) {
        @files = glob "*";//I am getting the list of files
    } else {
        @files = ();
    }

    $run{'files'} = \@files;
    $run{'testing'}= 'files';

    return 'site/screen/screen.tt',\%run;
}

模板工具包文件:(Example.tt)

//This is The condition to display the Upload functionality in the .tt file
            [% IF screenName == 'Resource Management' %]
//This is the code given in Stackoverflow,for displaying the list of files getting from    perl file to .tt file.But this functionality is not working in this .tt file.
[% files %]
[% FOREACH n IN files %]
    [% n %]
    [% END %]
//This is the Table format to display the Upload functionality in the .tt file.
<table id='dataTableExample' class=dataTable cellpadding=01 cellspacing=01>

        <tr class=verification style="text-align:left;">
            <th colspan="2">Instrumentation Configuration</th>
        </tr>
    <tr class=controlTableDataRow>
                                    <td class=controlTableCommandCell>
                                             <form    action='/sa/site/screen/testresults/ajaxTab/test/[% parentid %]/[% id %]' method='post'   enctype="multipart/form-data" target='file_upload'>
                                                    <input type="file"  name="uploadFile" size=30>
                                                    <input type="submit" name="submit" value="Upload">
                                                    <iframe id='file_upload' name='file_upload' style="display:none" src='about:blank' onload="if (file_upload.location.href != 'about:blank') uploadStatus(this)" >
                                                    </iframe>
                                            </form>
                                    </td>
                            </tr>
//This is the sample code to display in the .tt file
    <table class=propertyTable cellpadding=5 cellspacing=0>
    <tr class=propertyTableHeaderRow>
            <th>FileName</th>
            <th>Last Modified Date</th>
    </tr>
    </table>
[% END %]

For your reference i am providing the complete file,please help to solve this problem  for displaying the list of files in .tt file.

【问题讨论】:

  • 实际调用TT渲染模板的代码在哪里?
  • 您在使用 Catalyst 吗?

标签: perl template-toolkit


【解决方案1】:

这个结构很适合你的目标:

use strict;
use warnings;

use Template;

my @files = glob "*";
my $tt = Template->new();
my $va = {
    files => \@files
};
$tt->process('my.tt', $va);

在 my.tt 文件中:

[% FOREACH n IN files %]
[% n %]
[% END %]

【讨论】:

  • 感谢您的回复。我尝试通过放入我的代码来使用相同的代码,但我没有在视图上得到任何显示(使用模板工具包文件)...他们有什么问题吗我的代码或任何语法错误。基本上我无法将 perl 文件的结果返回到模板工具包文件。因为我能够在日志中获取文件列表,但不能在模板文件中获取。
  • 我放置 my.tt 文件供您参考
  • 嗨,谁能帮我提供上述问题的解决方案。我在他们提供.tt文件和perl文件。谢谢。
  • [% IF screenName == 'Resource Management' %] 有效吗?如果没有,那么您将看到什么都不会起作用。
【解决方案2】:

我意识到这是一个非常古老的问题,但我只是遇到了它,并且相信我看到了这个问题。您的代码中似乎有一个额外的 [% END %] 上限,接近尾声。我将此代码放入模板中,它做了与您看到的相同的事情。删除了最后一个 [% END %] ,我得到了输出。显然,由于我没有处理,它不是您想要的相同输出,但它解决了 TT 消隐问题。我确定您的问题很久以前就解决了,但希望这可以在将来为其他人节省一些麻烦。那些流氓标签对于 TT 来说可能会很痛苦。

【讨论】:

    猜你喜欢
    • 2019-05-29
    • 2019-07-12
    • 2012-09-06
    • 2014-01-30
    • 1970-01-01
    • 2015-07-06
    • 1970-01-01
    • 2015-12-07
    • 2012-03-10
    相关资源
    最近更新 更多