Using PHP as a template engine

I will not argue about choosing a template engine for PHP only. I prefer not to use a template engine, such as Smarty, because I would like to learn how to properly create a template using PHP and HTML. Can someone provide links or examples on how to create a template page?

+3
source share
6 answers

Just use the alternative PHP syntax for if / for / foreach language constructors that are specifically designed 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 reusing HTML code.

+15
source

It really is not that difficult.

Non-PHP goes out here
<?php # PHP goes in here ?>
More non-PHP goes out here
<?php # More PHP goes in here ?>
+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 , , :

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

:

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

, MVC-, ( ), $this , .

, - , .. (, ).

+1

I used various template engines and developed my own, and over time it became more complex. I believe that it is best to use it as simple as possible, using your own php material, instead of creating complex functions. (there are some good points in this article: Drilling architecture is good ). What I found was much better to read and maintain, returning to the project after a few months or years.

For instance:

<?
$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;
0
source

Using Richard's example, but simpler:

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

Source: https://habr.com/ru/post/1791656/


All Articles