5

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?

Ashley Medway
  • 7,151
  • 7
  • 49
  • 71
Shoe
  • 74,840
  • 36
  • 166
  • 272
  • All depends on how far you want to go in separating PHP from the template. Do you accept having to put variables in short tags (`=$var?>`). Do you accept loops to walk through an array? Or do you want to keep the templates free from PHP and substiture variables in another way. You apparently know some template engines. Why not peek in the source and learn how they work. Better than whining if people don't post a complete TE in their first reply. – GolezTrol Feb 12 '11 at 10:49
  • Here you go: http://stackoverflow.com/questions/3988627/using-template-on-php/3989380#3989380 – Your Common Sense Feb 12 '11 at 10:29
  • check the comments here http://php.net/manual/en/control-structures.alternative-syntax.php – Andrew Jan 13 '17 at 18:20

6 Answers6

18

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.

mwjohnson
  • 661
  • 14
  • 26
Richard Knop
  • 81,041
  • 149
  • 392
  • 552
  • 5
    Why not mentioning = ... ?> (equivalent to ) ? Now its enabled about everywhere and seems to perfectly fit this use case. – Fabian Pijcke Apr 27 '15 at 08:20
9

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 ?>
Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
9
 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>
Edmhs
  • 3,645
  • 27
  • 39
  • This example is very dangerous as it is very prone to causing XSS vulnerabilities. See for reference: https://cheatsheetseries.owasp.org/cheatsheets/Cross_Site_Scripting_Prevention_Cheat_Sheet.html – ALZlper Apr 09 '23 at 05:05
2

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 ?>
Maxwell s.c
  • 1,583
  • 15
  • 29
1

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

Denis 'Alpheus' Cahuk
  • 4,152
  • 2
  • 23
  • 31
1

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="john@xyz.com";

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;
timh
  • 1,572
  • 2
  • 15
  • 25