Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
Here is your job.
And here is the code obtainable via hg clone http://hg.metaperl.com/seamstress

Driver script

use strict; use warnings; use Model; use View::bullet; use View::table; my $model = Model->new; # View 1 my $view = View::bullet->new; $view->render($model); warn $view->as_HTML; # View 2 my $cols = 3; my $tabular_model = $model->reform_data($cols); my $view = View::table->new; $view->render($tabular_model); warn $view->as_HTML;

Model package

package Model; use Array::Group; use Data::Dumper; # A model is overkill for this example, but lets plan for # scaleability sub new { my $data = [1 .. 10] ; bless $data, __PACKAGE__ ; return $data; } sub reform_data { my $aref = shift; my $cols = shift; my $tabdata = Array::Group::ngroup $cols => $aref ; # This filling of the last row should be an option to # Array::Group... my $last_row = $tabdata->[$#$tabdata] ; my $diff = $cols - @$last_row; my @nbsp = (' ') x $diff; push @$last_row, @nbsp; return $tabdata; } 1 ;

View code

bullet

package View::bullet; use base qw(HTML::Seamstress); my $file = 'html/bullet.html'; sub new { __PACKAGE__->new_from_file($file); } sub render { my $tree = shift; my $model = shift; my $li = $tree->look_down(class => 'nums'); $tree->iter($li => @$model) ; return $tree; } 1;

table

package View::table; use base qw(HTML::Seamstress); my $file = 'html/table.html'; sub new { __PACKAGE__->new_from_file($file); } sub render { my $tree = shift; my $data = shift; $tree->table2 ( table_data => $data, td_proc => sub { my ($tr, $data) = @_; my @td = $tr->look_down('_tag' => 'td'); for my $i (0..$#td) { $td[$i]->splice_content(0, 1, $data->[$i]); } } ) ; return $tree; } 1;
I have beheld the tarball of 22.1 on ftp.gnu.org with my own eyes. How can you say that there is no God in the Church of Emacs? -- David Kastrup
[tag://html,templating]
Enforce strict model-view separation in template engines via HTML::Seamstress The car is in the cdr, not the cdr in the car

In reply to Re^2: object-oriented composition of model-view with HTML::Seamstress by metaperl
in thread RFC - Template::Empty by redhotpenguin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others rifling through the Monastery: (6)
    As of 2014-12-28 18:40 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (182 votes), past polls