Re: templating engine options
| From: | Nathan Rixham | Date: | Mon, 25 May 2009 16:17:46 +0000 |
| Subject: | Re: templating engine options | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-293141@lists.php.net to get a copy of this message | ||
Tom Worster wrote:
On 5/25/09 10:04 AM, "Stuart" <stuttle@gmail.com> wrote:that's like smarty too <div class="option {$option_class}"> ... </div> $s->assign('option_class', empty($option_class) ? '' : $option_class); identical infact - like the way you moved the logic out of the template in that example though :pQuick question, how would you implement the following using your XML-based template syntax... <div class="option <?php if (!empty($option_class)) { echo $option_class; } ?>"> ... </div> It's worth noting that I'm simply suggesting a different way of looking at the world. If you have a templating system you're happy with then feel free to continue using it, but I'd encourage you to take the time to consider whether it actually gives you any tangible benefits. I've used Smarty as well as a number of proprietary templating systems and I'm yet to come across one that can justify its existence over simply using PHP. It's also worth noting that when I refer to a "templating system" I mean something that introduces an extra step when running a template. I consider the template classes I use to be a templating system but they do nothing but formalise the process of passing data to other PHP scripts and providing utility functions for them to use.revisiting the template question is interesting. i'm grateful for this discussion. as far as your question goes, with phplib-template, i'd do: <div class="option {option_class}"> ... </div> and in the php it is: $t->setVar('option_class', empty($option_class) ? '' : $option_class); or some equivalent code. $t is the template object.
the interesting part is trying to explain why i like this better than your method. perhaps it's because it reduces the page assembly task to a sequence of regexp replacements and i'm more comfortable with that than i am with passing variable state to an included php file. why would i be? maybe i'm more comfortable looking at the world as nothing but strings and in this world, manipulating them is my job. there's certainly something i like about not having any php in my html templates but i can't say exactly what.snap - I think it's old sites and big nasty chunks monolithic code intertwined that any fool or genius could break in a second, but couldn't read for the life of them. for this reason purely, a template engine is a good idea as it enforces the good practise.
i don't think i could come up with better justifications for my current methods without trying our the alternatives for a decent period.I've played a lot of template things in the past - the only thing I thought was great was xsl, but not many people (comparatively) know xsl. Likewise with actionscript (flash/flex) but not even getting in tot hat as it's way outside of scope. i think the test will be the template engine, langauge, syntax, whatever that can best hand this: <?php if( isset($comments) && is_array($comments) && count($comments) > 0 ) { echo '<h2>Comments</h2>'; echo '<div id="comments">'; foreach( $comments as $index => $comment ) {
echo '<a href="' . $comment->link . '">';
echo $comment->title;
echo '</a>';
}
echo '</div>';
} else {
echo '<h3>No Comments</h3>';
}
?>
without any php or escaping in and out of.
I know it's pre-hypertext processor and supposed to spit out html - but I REALLY don't want to ever spit out a single html element from a php script ever - nothing but raw data, a raw html template separately and then *something* in the middle to make the magic happen.