Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Abstracting away layout details when using large HTML::Template-based sites?

by wfsp (Abbot)
on Dec 09, 2007 at 08:37 UTC ( #655944=note: print w/ replies, xml ) Need Help??


in reply to Abstracting away layout details when using large HTML::Template-based sites?

Load the title param into $page before you load $page->output into $layout

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; $Data::Dumper::Indent = 1; $Data::Dumper::Sortkeys = 1; use HTML::Template; # # Load the layout and a stub page of "content" # my $layout = HTML::Template->new( filename => "layout.template" ); my $page = HTML::Template->new( filename => "page.inc" ); $page->param( title => "STEVE"); # <-- new line # # Insert the body into the layout template # $layout->param( page => $page->output() ); # # Now setup the title # $layout->param( title => "STEVE" ); print $layout->output();
output:
<html> <head> <title>STEVE</title> </head> <body> <h2>STEVE</h2> <p>Imagine content here ..</p> </body> </html>
update:
Add a little more abstraction and feel even more pleased (possibly). :-)
#!/usr/bin/perl use strict; use warnings; use HTML::Template; my $page_params = { title => "STEVE" }; my $layout_params = { title => "STEVE", page => page($page_params), }; my $layout = HTML::Template->new( filename => "layout.template" ); $layout->param($layout_params); print $layout->output(); sub page { my ($args) = @_; my $page = HTML::Template->new( filename => "page.inc" ); $page->param($args); return $page->output; }


Comment on Re: Abstracting away layout details when using large HTML::Template-based sites?
Select or Download Code
Re^2: Abstracting away layout details when using large HTML::Template-based sites?
by skx (Parson) on Dec 09, 2007 at 14:10 UTC

    For my use case I don't know in advance which items to set in the header and which in the page, so this solution doesn't really scale past the small example. (Much like moving the title as suggested by ww initially failed to solve the general problem).

    Steve
    --
      HTML::Template puts together html very nicely. Everything else should be done by your script. I think you are trying to get the template to do too much.

      Your "content will be coming from... a database" so you could add a field which specifies "which items to set in the header and which in the page" or alternatively put that information in your script/config file i.e. one way or another, make a table.

      #!/usr/bin/perl use strict; use warnings; my %config = ( page => { tmpl => q{page.tmpl}, params => [qw{title param1 param2}], }, layout => { tmpl => q{layout.tmpl}, params => [qw{title param3 param4}], }, ); my %all_params = ( title => q{title}, param1 => q{param1}, param2 => q{param2}, param3 => q{param3}, param4 => q{param4}, ); my %page_params = map {$_ => $all_params{$_}} @{$config{page}->{para +ms}}; my %layout_params = map {$_ => $all_params{$_}} @{$config{layout}->{p +arams}}; print Dumper \%page_params; print Dumper \%layout_params;
      output:
      $VAR1 = { 'param2' => 'param2', 'title' => 'title', 'param1' => 'param1' }; $VAR1 = { 'param4' => 'param4', 'param3' => 'param3', 'title' => 'title' };
      Everything is in your code where, imo, it should be and then let HTML::Template do what it is very good at, whacking out nice HTML.

      Using a filter on a template is, imo, rather missing the point but ymmv.

      Perl data structures are the best thing since sliced bread, let Perl take the load. :-)

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (13)
As of 2014-09-18 14:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (116 votes), past polls