Jose Lee
Jose Lee

Reputation: 15

Output all names from an array in HTML

I am trying to loop and get all arrays, every name with its own <li> element.

<li>
#Foo
with all div and span inside here
</li>

<li>
#bar
with all div and span inside here
</li>

<li>
#moo
with all div and span inside here
</li>
use CGI::Pretty ":standard";

@names  = ('Foo', 'Bar', 'Moo');
my $str = join(" ",@names);

$data =  li (
           div({"class='inforno'"}, "<img src='inforno>'"),
           div({"class='inforno'"}, "<a href='#' class='inforno'>$str<
+/a>"),
           span({"class='inforno'"},  "<a href='#' class='inforno'>
                   Edit user </a>")

          );

     print "$data\n";

when I run I get only single <li> element with all names in the same <li>. I want to give each name its own <li> with all <div> and <span> elements inside in opening and closing <li> tags.

Upvotes: 0

Views: 80

Answers (2)

Borodin
Borodin

Reputation: 126742

Unless you are using a very old copy of CGI::Pretty, your code produces this warning message when you run it

CGI::Pretty is DEPRECATED and will be removed in a future release. Please see https://github.com/leejo/CGI.pm/issues/162 for more information at C:/Strawberry/perl/site/lib/CGI/Pretty.pm line 21.

It is important that you don't ignore warning messages: they are there to help you

You will also get three occurrences of this warning

Odd number of elements in anonymous hash ...

That is because you are using {"class='inforno'"} which is the wrong syntax for an anonymous hash

Because CGI is deprecated, and because the HTML generation parts of the library are especially frowned on, it is best to use a templating system. One of the most popular templating systems is the Template::Toolkit module, and this code shows you how to use it to achieve what you want

Perl

use strict;
use warnings 'all';

use Template;

my @names  = qw/ Foo Bar Moo /;

my $template = Template->new;

binmode STDOUT;

$template->process('list.html', { list => \@names })
        or die $template->error;

list.html

[% FOREACH name IN list %]
<li>
  <div class='inforno'>
    <img src='inforno'>
  </div>
  <div class='inforno'>
    <a href='#' class='inforno'>[% name %]</a>
  </div>
  <span class='inforno'>
    <a href='#' class='inforno'>Edit user</a>
  </span>
</li>
[% END %]

output

<li>
  <div class='inforno'>
    <img src='inforno'>
  </div>
  <div class='inforno'>
    <a href='#' class='inforno'>Foo</a>
  </div>
  <span class='inforno'>
    <a href='#' class='inforno'>Edit user</a>
  </span>
</li>

<li>
  <div class='inforno'>
    <img src='inforno'>
  </div>
  <div class='inforno'>
    <a href='#' class='inforno'>Bar</a>
  </div>
  <span class='inforno'>
    <a href='#' class='inforno'>Edit user</a>
  </span>
</li>

<li>
  <div class='inforno'>
    <img src='inforno'>
  </div>
  <div class='inforno'>
    <a href='#' class='inforno'>Moo</a>
  </div>
  <span class='inforno'>
    <a href='#' class='inforno'>Edit user</a>
  </span>
</li>

Upvotes: 4

Will Sheppard
Will Sheppard

Reputation: 3479

You don't need a join, you need to use a foreach loop and put the code that generates and prints the <li> inside the loop.

use CGI::Pretty ":standard";

@names  = ('Foo', 'Bar', 'Moo');

foreach $name (@names){
    $data =  li (
            div({"class='inforno'"}, "<img src='inforno>'"),
            div({"class='inforno'"}, "<a href='#' class='inforno'>$name</a>"),
            span({"class='inforno'"},  "<a href='#' class='inforno'>
                    Edit user </a>")

            );  

        print "$data\n";
}

Upvotes: -1

Related Questions