开发者

PHP Content Separation

So we all know that you should always, not only in PHP, separate code from content/design/html. (I have seen people that say the opposite here today)

I mean, you don't want one of these in bigger projects, do you?

<?php

echo '<div id="blah"><b>'
     . $username . '</b>'
     . $stuff . '<more HTML mixed with PHP...>';

?>

But: What is a good approach to separate code from content?

I have been 开发者_运维问答using a simple template system that replaces wildcards in templates mostly.

e.g:

<div id="blah"><b>%USERNAME%</b>%STUFF% <...>

(located in a single file)

Later you can just call a function similar to GetTemplate( 'myTemplate', array ( 'USERNAME' => 'Stranger' ) );

Finally, the questions:

  • Is this a good way of separating code and content?
  • How do you do that when not working with a framework?
  • Is there a better way?


you should realize that PHP itself is a templating language. your

<div id="blah"><b>%USERNAME%</b>%STUFF% <...>

differs from

<div id="blah"><b><?php echo $USERNAME; ?></b><?php echo $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.


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' ) );

function GetTemplate($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); ?>

function link_to($name, $url, array $attributes = array())
{
  $attributes['href'] = urlencode($url);
  foreach($attributes as $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.


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.


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜