简体   繁体   中英

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?

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?

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:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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