问题描述
我不打算仅针对 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?
推荐答案
对于专门为此目的而设计的 if/for/foreach 控制语言结构,只需使用替代 PHP 语法:
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; ?>
我还建议为非常相似的 HTML 输出创建视图助手,并使用它们而不是重复的 HTML 代码.
I also suggest creating view helpers for HTML outputs that are very similar and use them instead of having repeated HTML code.
这篇关于使用 PHP 作为模板引擎的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!