简体   繁体   中英

Unable to return the perl Hash into the template toolkit file

Thanks in advance. I am trying to display the list of files using the Template Toolkit file.Before this i am returning the hash from perl file into the .tt(template toolkit file) file. But the condition is not executing and also i am unable to display the list of files. For your reference i am providing the files.

Perl file (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;
}

Template toolkit file:(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.

This construction works fine for your aim:

use strict;
use warnings;

use Template;

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

In my.tt file:

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

I realize this is a very old question, but I just ran across it, and believe I see the issue. It would appear that you had an extra [% END %] cap in your code, toward the end. I threw this code into a template, and it did the same thing you're seeing. Removed the last [% END %] and I got output. Obviously since I don't have the processing, it wasn't the same output you'd like, but it solved the issue of TT blanking. I'm sure your problem was resolved long ago, but hopefully this saves someone else some trouble in the future. Those rogue tags can be a pain with TT.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM