in reply to How do I run a loop within a PSGI application

To follow up 1nickt's recommendation that you should use a templating system, consider:

use strict; use warnings; use HTML::Template; my $app = sub { my $htmlFile = genHTML_File(); my @options = map {{value => $_}} 'happy', 'sad', 'worrried'; my $tplt = HTML::Template->new(filename => $htmlFile); $tplt->param(options => \@options); return [200, ['Content-Type' => 'text/html'], [$tplt->output()]]; }; print $app->()[2][0]; sub genHTML_File { my $filename = 'delme.html'; open my $outHTML, '>', $filename or die "Can't create '$filename': + $!\n"; print $outHTML <<HTML; <html> <head></head> <body> <select><TMPL_LOOP name='options'> <option value='<TMPL_VAR name="value"/>'><TMPL_VAR name='value'/>< +/option></TMPL_LOOP> </select> </body> </html> HTML return $filename; }

Prints:

<html> <head></head> <body> <select> <option value='happy'>happy</option> <option value='sad'>sad</option> <option value='worrried'>worrried</option> </select> </body> </html>

In real code genHTML_File doesn't exist. Instead you put the HTML into an external file.

Premature optimization is the root of all job security

Replies are listed 'Best First'.
Re^2: How do I run a loop within a PSGI application
by tiny_monk (Sexton) on Aug 18, 2015 at 07:45 UTC

    Kevbot, GrandFather, I would like to thank you for sharing your codes and suggestions. All of you you are successfully teaching me the benefits of using a templating system. It's a lot easier to revisit a Perl code when the HTML code is separated from it. I will look into Template::Toolkit and HTML::Template.. Though, I wonder if either one could run on PSGI/Plack. Do you have any recommendations where good templating system is concerned? Anyway, I'll study the code that both of you had proposed. Thank you.

      HTML::Template is a lot easier to learn and use for simple applications than Template::Toolkit, at least in my opinion. However TT (Template::Toolkit) is widely used (and abused) with a lot more "power" (read - "rope to hang yourself with") than HTML::Template.

      Premature optimization is the root of all job security

        Hi Grandfather, I concur with your assessment of Template Toolkit and HTML::Template as regards complexity. However, as shown in Kevbot's reply, Template::Tiny is comparable to HTML::Template in offering a basic feature set without enough rope to hang oneself. And it also provides the upgrade path to TT, so templates made for it will still work once you upgrade for the full feature set. And if you are lazy, the syntax requires less typing, as a template variable is written [% varname %] rather than <TMPL_VAR NAME=varname>

        I recommend checking out Template::Tiny; It makes a lot more sense than using the venerable (and perfectly capable) HTML::Template IMHO. (As an aside, even though the author named it HTML::Template, it can be used, like TT and Template::Tiny, to generate any document from a template.)

        The way forward always starts with a minimal test.