Using PHP as a template engine

I am not going to argue about the choice of a template engine against only PHP. I choose not to use a template engine, like Smarty, because I would like to learn how to properly design a template using PHP and HTML. Could someone provide links or examples on how to design a template page?


Just use alternative PHP syntax for if/for/foreach control language constructs which are designed specifically for this purpose:

    <h1>Users</h1>
<?php if(count($users) > 0): ?>
    <table>
        <thead>
            <tr>
                <th>Id</th>
                <th>First Name</th>
                <th>Last Name</th>
            </tr>
        </thead>
        <tbody>
<?php foreach($users as $user): ?>
            <tr>
                <td><?php echo htmlentities($user->Id); ?></td>
                <td><?php echo htmlentities($user->FirstName); ?></td>
                <td><?php echo htmlentities($user->LastName); ?></td>
            </tr>
<?php endforeach; ?>
        </tbody>
    </table>
<?php else: ?>
    <p>No users in the database.</p>
<?php endif; ?>

I also suggest creating view helpers for HTML outputs that are very similar and use them instead of having repeated HTML code.


It's really not all that difficult.

Non-PHP goes out here
<?php # PHP goes in here ?>
More non-PHP goes out here
<?php # More PHP goes in here ?>

 function returnView($filename,$variables){
    ob_start();
        $htmlfile = file_get_contents($filename);  
        foreach($variables as $key=>$value){
          $htmlfile = str_replace("#".$key."#", $value, $htmlfile);
        }              
        echo $htmlfile;
    return ob_get_clean(); 
 } 

//htmlfile
<html>
<title>#title#</title>
</html>


//usage

echo returnView('file.html',array('title'=>'hello world!');

im my framework i have function that loads view, and then outs it in layout:

 public function returnView(){
    ob_start();
    $this->loader();
    $this->template->show($this->controller,$this->action);
    return ob_get_clean(); 
 }

Layout looks like this:

<html> 
    <head>
        <title><?php echo $this->layout('title'); ?></title>
    </head>
    <body>
        <?php echo $this->layout('content'); ?>
    </body>
</html>

What you might want to consider, if you should opt for a MVC-style approach, if you include your templates inside an object (one of its class methods) then $this inside the template file will point to the object you called it from.

This can be very useful if you want to ensure some kind of encapsulation for your templates, i.e. if you do not want to rely on global variables to pass around dynamic data (e.g. from a database).


Using Richard's example, but more simple:

    <h1>Users</h1>
<? if(count($users) > 0): ?>
    <table>
        <thead>
            <tr>
                <th>Id</th>
                <th>First Name</th>
                <th>Last Name</th>
            </tr>
        </thead>
        <tbody>
<? foreach($users as $user): ?>
            <tr>
                <td><?= htmlentities($user->Id) ?></td>
                <td><?= htmlentities($user->FirstName) ?></td>
                <td><?= htmlentities($user->LastName) ?></td>
            </tr>
<? endforeach ?>
        </tbody>
    </table>
<? else: ?>
    <p>No users in the database.</p>
<? endif ?>