Please take our Survey
logo       

Choosing A Webhost:
A web hosting service is a type of Internet hosting service that allows individuals and organizations to provide their own website accessible via the World Wide Web. Web hosts are companies that provide space on a server they own for use by their clients as well as providing Internet connectivity, typically in a data center. Web hosts can also provide data center space and connectivity to the Internet for servers they do not own to be located in their data center, called colocation. more...

Re: Alternate loops: msg#00023

lang.perl.modules.html-template

Subject: Re: Alternate loops

Mathew Robertson wrote:
> Hi Jonathan,
>
> I think the exampe of:
>
> @loop = (
> ...
> {value_set => 2, count_distribution => 1, sum_of_counts => 36 },
> ...
> )
>
> is invalid, since you never generate the loop contruct like that.

"invalid" may be too strong of a word; maybe you mean "unlikely"?

For brevity's sake, I was describing the contents of a variable that's
about to be loaded into the template; I was not intending to address
the question of how it gets put together in the first place. That
said:

> Usually you would do something like:
>
> for ($min..$max) { # - loop through to max rows
> push @loop, { # \
> value_set => $_, # - make row values
> count_distribution => make_value($_), # /
> };
> }
> $ht->param(some_loop => \@loop); # - make rows available
> $ht->param(sum_of_counts => 36); # - make max count available
>
> The point being that usually you build up loop content dynamicaly, say from a
> database
> or a computation, rather than simply outputting static content.

True, but not a point that has much of an impact on my proposal.

Take the example given in the documentation:

my @words = qw(I Am Cool);
my @numbers = qw(1 2 3);
my @loop_data = ();
while (@words and @numbers) {
my %row_data;
$row_data{WORD} = shift @words;
$row_data{NUMBER} = shift @numbers;
push (@loop_data, \%row_data);
}
$template->param(THIS_LOOP => \@loop_data);

--

<TMPL_LOOP NAME="THIS_LOOP">
Word: <TMPL_VAR NAME="WORD"> <br>
Number: <TMPL_VAR NAME="NUMBER"> <p>
</TMPL_LOOP>

Everything after the "my @numbers" line and before the
"$template->param" line is an example of having to jump through hoops
in order to get the information into a "table format" that H::T can
use. Furthermore, the related template is under some severe
limitations, in that information that has not been put into @loop_data
cannot be displayed from inside a <TMPL_LOOP> tag.

Looking at the same example, but implementing my proposal, you'd have this:

my @words = qw(I Am Cool);
my @numbers = qw(1 2 3);
$template->param(WORDS => \@words);
$template->param(NUMBERS => \@numbers);

--

<TMPL_LOOP>
Word: <TMPL_VAR NAME="WORDS"> <br>
Number: <TMPL_VAR NAME="NUMBERS"> <p>
</TMPL_LOOP>

Much simpler, no?

> Also, I'm not confident that H::T could be extended with the
> list-of-list case (without some re-work), as H::T uses
> recursion for TMPL_LOOPS (eg: when a loop begins, a new
> instance of H::T is created -> it becomes the current context).
> Note that most of my templates use lists-of-lists to produce
> tables, so I do consider this to be a must have.

See, that "current context" bit is something else that I'd like to get
away from with unnamed template loops. Going back to what you have in
the second quote above: if I use <TMPL_LOOP NAME="some_loop">, I can
only use <TMPL_VAR NAME="value_set"> and <TMPL_VAR
NAME="count_distribution"> within it; I cannot use <TMPL_VAR
NAME="sum_of_counts"> in there, because "sum_of_counts" isn't in the
current context. Unnamed template loops wouldn't change the current
context, so I'd be able to make use of any or all of the appropriate
arrays or scalars within it.

> The idea of unnamed template loops is a interesting idea, but
> all it really does is to increase the complexity of H::T, for
> no real increase in useability.

I'll have to beg to differ. As things stand, the script writer has to
have a strong feel for how the data that he provides is going to be
used, and needs to set up the lists of hashes accordingly. The
template writer is then constrained to display each list-of-hashes in
a separate loop, even if he thinks that the data makes more sense
bundled together in a single loop. And that's not even mentioning the
"current context" constraint.
With unnamed template loops, the template writer is free to bundle as
much or as little of the available data into a given loop as he feels
is appropriate. If that's not a real increase in useability, what is?

> PS. In all of my templates, I know of exactly _zero_ cases where I could use
> un-named
> loop variables.

Correction: Every last case you know of _could_ use unnamed loops,
since unnamed loops can do everything that regular loops can. Not one
of your cases _does_, obviously, since unnamed loops aren't currently
implemented; but that's not the same thing. And I can think of a
number of cases where unnamed loops would streamline the script
writer's job without complicating the template writer's job.

--
Jonathan "Dataweaver" Lang


-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems? Stop! Download the new AJAX search engine that makes
searching your log files as easy as surfing the web. DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_idv37&alloc_id865&op=click


<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

Recently Viewed:
qnx.openqnx.dev...    gcc.libstdc++.c...    solaris.opensol...    information-ret...    misc.misterhous...    web.catalyst.ge...    apache.webservi...    redhat.release....    hardware.lirc/2...    kernel.autofs/2...    technology.sust...    linux.vdr/2003-...    editors.lyx.gen...    org.user-groups...    netbsd.devel.pk...    xdg.devel/2004-...    version-control...    jakarta.slide.d...    debian.packages...    creativecommons...    ports.ppc.embed...    bug-tracking.bu...   
Home | blog view | USPTO Patent Archive | advertise | OSDir is an inevitable website. super tiny logo

Free Magazines

Cisco News
Receive a free quarterly e-newsletter with exclusive articles on how Cisco IT uses its own products and solutions to enable the business.
subscribe

Systems Management News, the newspaper for IT systems administration and data center managers! Each issue of Systems Management News is chock-full of news and analysis to help you understand what's happening in your field.
subscribe

The Enterprise Newsweekly eWeek is the essential technology information source for builders of e-business.
subscribe

Oracle Magazine Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Oracle (NASDAQ: ORCL) is the world's largest enterprise software company.
subscribe

Total Telecom Total Telecom is "The Economist of the communications industry".
subscribe