Using PHP as a template engine

后端 未结 6 2094
梦毁少年i
梦毁少年i 2020-12-02 00:57

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 prop

相关标签:
6条回答
  • 2020-12-02 01:08
     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>
    
    0 讨论(0)
  • 2020-12-02 01:09

    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 ?>
    
    0 讨论(0)
  • 2020-12-02 01:14

    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.

    0 讨论(0)
  • 2020-12-02 01:14

    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;
    
    0 讨论(0)
  • 2020-12-02 01:20

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

    0 讨论(0)
  • 2020-12-02 01:28

    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 ?>
    
    0 讨论(0)
提交回复
热议问题