简体   繁体   English

在其他html模板中使用html模板?

[英]Using html templates within other html templates?

Let's say we have a web app that let's users schedule appointments. 假设我们有一个网络应用程序,可让用户安排约会。 And each appointment has a standard block-like display template. 每个约会都有一个标准的块状显示模板。 Something simple like: 很简单的东西:

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

and we want to use that in multiple different places on the site... 我们想在网站上的多个不同地方使用它...

  • appointment creation page (single) 约会创建页面(单个)
  • appointment previews (single) 约会预览(单个)
  • user's list of appointments (repeated via loop) 用户的约会列表(通过循环重复)

DRY is important, so what is the best way to use this mini-template? DRY很重要,那么使用此迷你模板的最佳方法是什么?

For the third example, where we are looping, is it reasonable to do something like: 对于第三个示例,在我们循环的地方,执行类似以下操作是否合理:

foreach ($appointments as $appt) {
    include 'templates/appt.php';
}

with the template using the $appt array for filling in all the data? 使用$appt数组填充所有数据的模板?

I'm just getting started with templates so I'd like some advice. 我刚刚开始使用模板,所以我需要一些建议。

Thank you! 谢谢!

EDIT BY REQUEST: 按要求编辑:

In all 3 cases above the same html template would be pulled - appt.php - and it would look like this: 在相同的html模板上方的所有3种情况下, 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>

with h() being an abbreviated function for htmlspecialchars() that I use... h()是我使用的htmlspecialchars()的缩写函数...

您可能要在http://mustache.github.com/上查看有关PHP的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM