Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: HTML::Template : How to separate code and html with lesser maintenance issues

by jbrugger (Parson)
on Nov 21, 2006 at 05:51 UTC ( [id://585199]=note: print w/replies, xml ) Need Help??


in reply to HTML::Template : How to separate code and html with lesser maintenance issues

You could choose to create 3 different templates for select, continue or denied.
my $tmpl = ""; my $item = "CONTINUE"; if ($item eq "SELECT") { $tmpl = "select.tmpl"; } elsif ($item eq "CONTINUE") { $tmpl = "continue.tmpl"; } elsif ($item eq "DENIED"){ $tmpl = "denied.tmpl"; } my $template = HTML::Template->new( filename => $tmpl );
So no ifs are needed in your template (i try to put as less logic as possible in my templates as i can)
Do you want (simple) logic? Try HTML::Template::Expr.

However, you may wonder if you need it. Another nice trick you can do with HTML::Template; call a perl function by using parameters, so you don't even need to register a function like in HTML::Template::Expr.
# template.tmpl <TMPL_VAR NAME="function_xyz"> # Perl: my $tmpl = HTML::Template->new( filename => 'template.tmpl' ); my @all_params = $tmpl->param(); foreach (@all_params) { if ($_ =~ /^function_/i) { my ($a,$b) = split (/_/,$_,2); $tmpl->param($_ => $b() ); } } sub xyz { return "template value needed"; }


"We all agree on the necessity of compromise. We just can't agree on when it's necessary to compromise." - Larry Wall.

Replies are listed 'Best First'.
Re^2: HTML::Template : How to separate code and html with lesser maintenance issues
by blazar (Canon) on Nov 21, 2006 at 10:38 UTC
    if ($item eq "SELECT") { $tmpl = "select.tmpl"; } elsif ($item eq "CONTINUE") { $tmpl = "continue.tmpl"; } elsif ($item eq "DENIED"){ $tmpl = "denied.tmpl"; }

    In which case (I know you're adapting the OP's code, but) one would probably use a hash instead, especially if other options were possibly to be added. But of course this is just a complete side note.

Re^2: HTML::Template : How to separate code and html with lesser maintenance issues
by tinita (Parson) on Nov 21, 2006 at 12:25 UTC
    $tmpl->param($_    => $b() );
    what's this? this gives me a syntax error. do you wanted to use a subroutine-reference instead? like main->can($b)?
      hmm, yes, it's just an example i stripped from my own code, where i use <TMPL_VAR NAME="function_module_functionname">. there i do the following:
      eval("require $module"); $mod = $module->new(); $tmpl->param($_ => $mod->$function())
      But i see you got the idea :)
      "We all agree on the necessity of compromise. We just can't agree on when it's necessary to compromise." - Larry Wall.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://585199]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2024-04-19 20:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found