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