Shoe
Shoe

Reputation: 76240

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?

Upvotes: 5

Views: 8913

Answers (6)

Maxwell s.c
Maxwell s.c

Reputation: 1668

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 ?>

Upvotes: 2

Richard Knop
Richard Knop

Reputation: 83695

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.

Upvotes: 18

timh
timh

Reputation: 1590

I've used various template engines, and designed my own as well, getting more elaborate over time. I think its best to keep it as simple as possible by using native php stuff, instead of creating elaborate functions. (this article has some good points: Boring Architecture is Good). What I found was much better readability and maintenance when coming back to a project after months or years.

For example:

<?
$name="john";
$email="[email protected]";

require "templates/unsubscribe.php";

-- templates/unsubscribe.php --

<?
$o=<<<EOHTML

Hi $name, sorry to see you go.<BR>
<input type=input name=email value=$email> 
<input type=submit value='Unsubscribe'>
EOHTML;
echo $o;

Upvotes: 1

Edmhs
Edmhs

Reputation: 3731

 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>

Upvotes: 9

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).

Upvotes: 1

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798676

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 ?>

Upvotes: 9

Related Questions