假设我们有一个让用户安排约会的web应用程序。每个约会都有一个标准的块状显示模板。比如:
<div class="appt">
<div class="title">Encounter strange phone booth</div>
<div class="location">San Dimas Circle-K</div>
<div class="participant">Ted</div>
<div class="notes">Strange things are afoot...</div>
</div>
我们想在网站的多个不同位置使用它…
- 约会创建页面(单)
- 预约预览(单)
- 用户的约会列表(通过循环重复)
DRY很重要,那么使用这个迷你模板的最佳方式是什么呢?
对于我们正在循环的第三个例子,这样做是否合理:
foreach ($appointments as $appt) {
include 'templates/appt.php';
}
与模板使用$appt
数组填充所有数据?
我刚刚开始使用模板,所以我需要一些建议。
谢谢!
编辑请求:
在上述所有3种情况下,相同的html模板将被拉- appt.php
-它看起来像这样:
<div class="appt">
<div class="title">
<?php echo h($appt['title']); ?>
</div>
<div class="location">
<?php echo h($appt['location']); ?>
</div>
<div class="participant">
<?php echo h($appt['participant']); ?>
</div>
<div class="notes">
<?php echo h($appt['notes']); ?>
</div>
</div>
h()
是htmlspecialchars()
的缩写函数,我使用…
您可以在http://mustache.github.com/上查看PHP