使用PHP作为模板引擎

5

我不想就只使用PHP来争论选择模板引擎的问题。我选择不使用像Smarty这样的模板引擎,因为我想学习如何使用PHP和HTML来正确设计模板。是否有人能提供关于如何设计模板页面的链接或示例?


请看这里:https://dev59.com/y1HTa4cB1Zd3GeqPQU46#3989380 - Your Common Sense
一切都取决于您想在分离PHP和模板方面走多远。您是否接受必须将变量放入短标记(<?= $var?>)中?您是否接受循环遍历数组的方式? 或者,您想让模板没有PHP并以另一种方式替换变量。您似乎了解一些模板引擎。为什么不查看源代码并学习它们的工作方式。比起抱怨别人没有在第一次回复中发布完整的TE,这样做更好。 - GolezTrol
请查看此处的注释:http://php.net/manual/zh/control-structures.alternative-syntax.php - Andrew
6个回答

18

只需使用专门为此目的设计的if/for/foreach控制语言结构的替代PHP语法:

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

我还建议为非常相似的HTML输出创建视图助手,而不是重复使用HTML代码。

5
为什么没有提到 <?= ... ?>(相当于 <?php echo ... ?>)?现在它已经在各个地方启用了,看起来非常适合这种情况。请将其翻译为:为什么没有提及 <?= ... ?>(等同于 <?php echo ... ?>)?现在几乎所有地方都已经启用它,它似乎非常适合这种使用情况。 - Fabian Pijcke

9

这并不是很难。

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

1
这是什么?你在讽刺我吗? - Shoe
9
不,我很认真。在没有模板引擎的情况下,做法是以这种方式混合使用 PHP 和基础输出。 - Ignacio Vazquez-Abrams
我认为还有一些关于它的技巧。 - Shoe
@Charlie 不要太多了。只需要使用短标签和控制结构的可替代语法即可。 - Your Common Sense
3
唯一真正重要的是:始终记住,在模板中应尽可能少使用 PHP 代码。在业务逻辑部分定义所有必要的数据,不要在模板中评估任何变量等。 - Your Common Sense

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!');

在我的框架中,我有一个函数用于加载视图,并将其放置在布局中。
 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>

这个例子非常危险,因为它很容易引发跨站脚本攻击漏洞。请参考:https://cheatsheetseries.owasp.org/cheatsheets/Cross_Site_Scripting_Prevention_Cheat_Sheet.html - ALZlper

2
使用Richard的例子,但更简单:

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

我认为使用 echo 简写和添加一些分号不值得为一个六年前的问题写一个新答案,兄弟。 - Marijan
在大型项目中,这会产生很大的影响。你有什么建议? - Maxwell s.c

1

我使用过各种模板引擎,并且还设计了自己的模板引擎,随着时间的推移变得越来越复杂。我认为最好使用本地 PHP 来保持尽可能简单,而不是创建复杂的函数(这篇文章提出了一些好的观点:Boring Architecture is Good)。我发现当几个月或几年后回到项目时,代码更易读和维护。

例如:

<?
$name="john";
$email="john@xyz.com";

require "templates/unsubscribe.php";

-- 模板/取消订阅.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;

Heredoc?你有过语法高亮的想法吗? - Your Common Sense

1

如果您选择采用MVC风格的方法,您可能需要考虑的是,如果您将模板包含在对象内部(其中一个类方法),那么模板文件中的$this将指向您从中调用它的对象。

如果您希望确保模板的某种封装性,即如果您不想依赖全局变量来传递动态数据(例如来自数据库的数据),则这非常有用。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接