使用PHP作为模板引擎

发布于 2024-10-17 02:58:59 字数 100 浏览 1 评论 0原文

我不会争论只选择 PHP 的模板引擎。我选择不使用模板引擎,例如 Smarty,因为我想学习如何使用 PHP 和 HTML 正确设计模板。有人可以提供有关如何设计模板页面的链接或示例吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

猫瑾少女 2024-10-24 02:58:59

只需使用专为此目的而设计的 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 代码。

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.

┈┾☆殇 2024-10-24 02:58:59

这真的没有那么困难。

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

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 ?>
随风而去 2024-10-24 02:58:59
 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>
 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>
遥远的她 2024-10-24 02:58:59

使用理查德的例子,但更简单:

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

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 ?>
闻呓 2024-10-24 02:58:59

如果您应该选择 MVC 风格的方法,您可能需要考虑什么,如果您将模板包含在对象内(其类方法之一),则模板文件内的 $this 将指向你调用它的对象。

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

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

冷︶言冷语的世界 2024-10-24 02:58:59

我使用过各种模板引擎,也设计了自己的模板引擎,随着时间的推移变得越来越复杂。我认为最好通过使用本机 php 内容来使其尽可能简单,而不是创建复杂的函数。 (这篇文章有一些优点:无聊的架构是好的< /a>)。当几个月或几年后回到项目时,我发现可读性和维护性要好得多。

例如:

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

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文