>>>>> "BM" == Bob Mariotti <[EMAIL PROTECTED]> writes:
BM> Perhaps some readers can post some small examples of a PHP and/or perl BM> program(s) that actually do what Uri was just stating... that being BM> the separation of text (html) from logic (PHP/perl)? ok, here is a recent little templater i wrote that is very powerful for its size. i had a prototype (since lost but trivial to rewrite) of an improvement to this design that would allow for conditionals, includes, etc but that needs preprocessing and this one doesn't. yet it can handle loops, nested data structures, and even conditionals (but in the code - see the address 2 stuff). i know it is a toy but i like it. i am still going to use TT2 (which has all the many features i would want and too many more) for this project. i had fun writing this and it is being used in two tiny (1 or 2 templates each) projects and one of those is actually live and the other will be soon. have fun with it and comments welcome. uri this is the code in the module (it doesn't even have a package space since i wasn't planning to share it!). below is some of the code used to build the data structures and below that is some template html. there are some more comments on each below so scroll down. #################################### # quick and simple templater #################################### # basic template rules: # sections are marked with <%CHUNK_FOO%>...<%END_FOO%> # if the top level is a hash ref, then a key FOO is used to expand the # template. # else the top level should be an array ref and then that list is # looped over and the CHUNK is expanded for each element (which should # be hash refs) # when a chunk is expanded all <%FOO%> are replaced with their values # in the param hash # this supports nested CHUNKS as it is recursive. you can build # complex templates with many levels as long as you have a data tree # with the same structure. use Data::Dumper ; sub expand_template { my( $template, $params ) = @_ ; $template =~ s{<%CHUNK_(\w+)%>(.+?)<%END_\1%>} { ref $params eq 'HASH' ? expand_template_chunk( $2, $params, $1 ) : expand_template_list( $2, $params ) ; }sgex ; #print "CHUNK1 $template", Dumper $params ; if ( ref $params eq 'HASH' ) { $template =~ s{<%(\w+)%>} { defined $params->{$1} ? $params->{$1} : '' }ge ; } #print "CHUNK2 $template" ; return $template ; } sub expand_template_chunk { my( $template, $params, $chunk_name ) = @_ ; my $chunk_params = $params->{ $chunk_name } ; return '' unless $chunk_params ; return expand_template_list( $template, $chunk_params ) if ref $chunk_params eq 'ARRAY' ; #print "CHUNK3 $template", Dumper $params ; return expand_template( $template, $chunk_params ) ; } sub expand_template_list { my( $template, $list ) = @_ ; return join '', map expand_template( $template, $_ ), @{$list} ; } 1 ; this is the sub that expands the html template snippet below. it just builds up the param tree according to how the template is laid out. this is an invoice with rows of items and some individual values. it shows how the templater handles lists and chunks and scalar. $params are the form params. i know the looped copy could be done with a slice but i had previously been messing with casing at that point so i needed a normal expression assignment. note the conditional handling of appending <BR> for the address2. this (slightly) breaks the separation of text and code. if the preprocessor version were being used, that conditional could have been inside the text. now the question is, is that embedded minilanguage (conditionals, loops, includes, etc,) still coding? how much do the html monkeys need to know about coding logic? note the used of lvalue values! :) sub expand_invoice_form { my( $order ) = @_ ; foreach my $item ( @{$order->{ITEMS}} ) { $item->{PRICE} = format_money( $item->{PRICE} ) ; $item->{ITEM_TOTAL} = format_money( $item->{ITEM_TOTAL} ) ; } $order->{SUBTOTAL} = format_money( $order->{SUBTOTAL} ) ; $order->{SHIPPING} = format_money( $order->{SHIPPING} ) ; $order->{TOTAL} = format_money( $order->{TOTAL} ) ; #$html .= "<PRE>\n" . Dumper( $order ) . "</PRE>\n" ; my $params = $order->{params} ; $order->{$_} = $params->{$_} for qw( CUST_NAME ADDRESS1 ADDRESS2 CITY STATE ZIP COUNTRY EMAIL_LIST FEED_BACK ) ; $order->{ADDRESS2_BR} = $order->{ADDRESS2} . $order->{ADDRESS2} ? "<BR>\n" : '' ; my $invoice_tmpl = load_invoice_template() ; $_ = HTML::Entities::encode( $_ ) for values %{$order} ; my $expanded = expand_template( $invoice_tmpl, $order ) ; return $expanded ; } this html was originally generated by a crapola html gui thing (adobe golive) by someone else. amazingly those tools all suck. so this part was hand edited afterwards and i did formating changes (massive whitespace indents were removed) as well as templating it. <%CHUNK_ITEMS%> <tr height="21"> <td width="206" bgcolor="white" height="21"> <%ITEM%> </td> <td bgcolor="white" width="112" height="21" align="right"> <%PRICE%> </td> <td width="60" bgcolor="white" height="21" align="right"> <%QUANTITY%> </td> <td bgcolor="white" height="21" width="140" align="right"> <%ITEM_TOTAL%> </td> </tr> <%END_ITEMS%> <tr> <td rowspan="3" colspan="2" width="206" bgcolor="white" valign="top"> </td> <td width="60" bgcolor="white"> <font face="verdana,helvetica,arial" size="2">Subtotal:</font> </td> <td bgcolor="white" width="140" align="right"><%SUBTOTAL%> </td> </tr> <tr> <td width="60" bgcolor="white"> <font face="verdana,helvetica,arial" size="2">S/H:</font> </td> <td bgcolor="white" width="140" align="right" ><%SHIPPING%> </td> </tr> <tr height="23"> <td width="60" height="23" bgcolor="white"> <font face="verdana,helvetica,arial" size="2"> <b>Total:</b> </font> </td> <td height="23" bgcolor="white" width="140" align="right"> <b><%TOTAL%> </b> </td> </tr> -- Uri Guttman ------ [EMAIL PROTECTED] -------- http://www.stemsystems.com --Perl Consulting, Stem Development, Systems Architecture, Design and Coding- Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org _______________________________________________ Boston-pm mailing list [email protected] http://mail.pm.org/mailman/listinfo/boston-pm

