Php Content Separation
Solution 1:
you should realize that PHP itself is a templating language. your
<divid="blah"><b>%USERNAME%</b>%STUFF% <...>
differs from
<divid="blah"><b><?phpecho$USERNAME; ?></b><?phpecho$STUFF; ?> <...>
only very superficially. granted, PHP is quite stupid with its NOTICEs and WARNINGs instead of exceptions (a point Python has over PHP), and your custom implementation may more easily allow you to e. g. throw instead of producing a fatal error (oh the joys of Smarty) if putting logic into a template somewhere turns out to be the lesser of two evils (loops). you may want to throw if the template mentions an undefined variable (PHP would issue a NOTICE), etc.
Solution 2:
Im not fan ov additional templating languages (that replace wildcards) instead i like to keep my templates pure php so my vertion of what you have done would be:
<div id="blah"><b><?php echo $username ?></b><?php echo $stuff ?><...>
echo GetTemplate( 'myTemplate.php', array ( 'username' => 'Stranger', 'stuff' => 'Stuff' ) );
functionGetTemplate($templatePath, array$vars = array())
{
extract($vars);
ob_start();
include($templatePath);
return ob_get_clean();
}
I alos combine this with helper functions/object->methods as well for example:
<?php echo link_to($name, $url); ?>
functionlink_to($name, $url, array$attributes = array())
{
$attributes['href'] = urlencode($url);
foreach($attributesas$attrib => $value)
{
$attributes[$attrib] = $attrib."=\"$value\"";
}
return sprintf('<a %s>%s</a>', implode(" ",$attributes), $name);
}
I generally apply helpers like these to commonly used tags/structures as well as having a general purpose html tag one that looks something like content_tag($tag, $content, $attributes);
This helps me avoid a php echo for tons of attributes for random tags. I obviously dont use it for every html tag i use only for ones where it makes for better readability than a ton of echos.
As far as templating engines go i dont really see the benefit as php is templating language. As far as non-programmers/coders they have to leanr the syntax for a loop and a variable any how so i dont see how changing it to wildcards or the {} syntax of smarty adds anything.
Solution 3:
There is a tried-and-true Templating Engine for PHP called Smarty. It isn't part of a massive framework, which is a huge plus for me. Smarty supports caching and has other performance boosts over traditional find/replace templating due to it's "compilation" of template files.
Solution 4:
Big projects like, phpBB work using templates. Its slightly more complicated as they also allow for (for)lists, including other templates and other advanced features. You definitely want to separate code and content.
As a bonus this allows non programmers to make your html.
Solution 5:
The approach of the Zend Framework which uses PHP as the template language (which does not add an extra parsing overhead) seems like a good apporach
Another way I really like is the approach used by limonade framework which uses ob_start() and includes to actually handle the templates
Post a Comment for "Php Content Separation"