Tom Worster wrote:
On 5/25/09 8:48 PM, "Nathan Rixham" <nrixham@xxxxxxxxx> wrote:
Sancar Saran wrote:
<?php
$content = 'No Comments';
if(isset($comments) and is_array($comments) and count($comments) > 0 ) {
$content = '';
foreach( $comments as $index => $comment ) : $content. = "<a href='".
$comment->link."'>".$comment->title."</a>"; endforeach;
}
?>
<h2>Comments</h2>
<div id="comments">
<?=$content?>
</div>
index.php
ob_start();
require('template.php');
echo ob_get_clean();
I'm still do not understand for complex template system requirement.
I just _need_ the two abstracted.
php must have no html in it
html template must contain no php
i can achieve that separation in phplib using blocks, i.e blocks of template
that may be repeatedly replaced, hierarchically if so desired. presumably
smarty has something equivalent. but, naturally, this doesn't meet your
"pull" requirement.
hoever, i sometimes find that some mixing is inevitable. for example, if i
am listing music recordings but not using a table, just a list. my template
might be (leaving out the classes and ids. and humor me in my old-fashioned
tag preferences):
<!-- BEGIN songblock -->
<li><b>{artist}</b>, {disk} <i>{label}</i></li>
<!-- END songblock -->
that works but if my database entry for a given disk has no information on
the label then the html output looks a but nasty:
<li><b>Mika Miko</b>, We Be Xuxa <i></i></li>
which sort of works but offends my sensibilities. since phplib doesn't have
a way to make a chunk of template conditional (other than with a block,
which would become a pita as you can imagine from the above), i sigh, shrug
and move the <i></i> in the conditional in the script. oh well. purity is
idealistic.
a possible solution would be to define macros in the template, maybe...
{MACRO:label=, <i>{label}</i>}
that would give me an optional comma and get rid of the space if there's no
label data while keeping all html out of the script.
while this achieves the ideal of separation i'm not sure it's really more
practical than the impurity of having some html bits in the script. having
practical experience with just one approach i can only speak theoretically
of the other.
(btw: macros can in fact be done in phplib templates by putting each macro
in a template file of its own, but that doesn't sound too practical in the
long run, does it?)
on another different topic, i would make independence of the output language
a requirement of the template scheme. e.g. besides html, i use phplib
templates to generate plain text emails, sql files, json ajax responses,
whatever...
you know you just reminded me of something..
for the past couple of years I've been working regularly on a huge
website which was made totally and completely terribly, everything about
it was just dire (worst code I've seen)
however, eating my above word here, I've just realised that the mini
custom template engine they used (whilst heavy and crap) actually
implemented pull!
in template they'd have
%user_stories_2%
which would actually mean "run function user_stories_2 and place output
here" - if the function wasn't defined it just replaced it with ''
(nothing).
so.. that is in fact pull, in a simple way - it doesn't cater for
allowing you to control the html output or suchlike, but the
optimization features and the general process was implemented.
damn I'm having a learning day/week. also noticed yesterday that a v v v
important and big project at the day job (which was going wrong and slow
for agesssss) has actually been designed wrong - it's implemented an
"anemic domain model" and a tonne of services, which completely kills
the point of using a domain model - couldn't put my finger on it before
but did today! also realised my own ORM was pants because I'd designed
it wrong using inheritance and created a mess of cross cutting concerns
which means the data layer can't be replaced easily - in short I've
implemented:
$country->saveOrUpdate();
but it should be:
Persistence::saveOrUpdate( $country );
(thus allowing the persistence layer to be swapped out without business
logic or domain model needing to be changed)
sigh but joy!
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php