开发者

Have trouble with HTML::Template

foreach $result (keys %{$results_ref}){
    $source = $results_ref->{$result}->{abs_path};
    $source =~ s#/home/##;
    print "<div><img src=\"$source\" /></div>";
}

It seems HTML:Template only support basic syntax. How can I do logic like above with it?

UPDATE

An arrayref to hash is not always enough ,consider the following example:

print $hash{LEFT};
foreach $i ($hash{START}..$hash{END}){
    if($pager == $i){
        print "<span>$i<span>";
    }
    el开发者_如何学Pythonse {
        print "<span><a href=\"/index.pl?page=$i\">$i</a></span>";
    }

How would you do it with arrayref to hash?


If I understand your question correctly I think you are looking for the TMPL_LOOP tag.

Build an array of hashes, and pass that as a variable to param() call. Then use TMPL_LOOP to build what you were doing in the foreach.

 my $sources_loop = [];
 foreach $result (keys %$results_ref) {
    $source = $results_ref->{$result}->{abs_path};
    $source =~ s#/home/##;
    push(@$sources_loop, { source => $source });
 }

 $template->param(sourcesloop => $sources_loop);

Then in the template:

 <!--TMPL_LOOP NAME="sourcesloop"-->
     <div><img src="<!--TMPL_VAR NAME="source"-->" /></div>
 <!--/TMPL_LOOP-->

EDIT (response to Update):

Again, if I understand correctly, then I would use different keys to drive the logic of the template loop. You can have TMPL_IF as part of TMPL_LOOP

my $sources_loop = [];
foreach $i ($hash{START}..$hash{END}){
    if($pager == $i){
         push(@$sources_loop, { lone => $i });
    }
    else {
        push(@$sources_loop, { linked => $i });
    }
}

$template->param(sourcesloop => $sourcesloop, hashleft => $hash{LEFT});

Template:

 <!--TMPL_VAR NAME="hashleft"-->
 <!--TMPL_LOOP NAME="sourcesloop"-->
     <!--TMPL_IF NAME="lone"--><span><!--TMPL_VAR NAME="lone"-->"</span><!--/TMPL_IF-->
     <!--TMPL_IF NAME="linked"--><span><a href="/index.pl?page=<!--TMPL_VAR NAME="linked"-->"><!--TMPL_VAR NAME="linked"--></a></span><!--/TMPL_IF-->
 <!--/TMPL_LOOP-->

EDIT: updated to include $hash{LEFT}

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜