1

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

Drew
  • 6,208
  • 10
  • 45
  • 68
  • 1
    Have you tried a templating engine yet? – Ignacio Vazquez-Abrams Nov 08 '11 at 01:11
  • 1
    I think using that loop is a pretty acceptable way of doing things. However, if you are looking into more complex inheritance in the future, I would suggest looking at the twig template engine: http://twig.sensiolabs.org/ – F21 Nov 08 '11 at 01:15
  • 1
    PHP is not a templating engine, regardless of what you may think. – Ignacio Vazquez-Abrams Nov 08 '11 at 01:16
  • @phpdev - something about looping an include sets off an alarm bell in my novice head... it just looks like a potential problem. Is this standard practice? – Drew Nov 08 '11 at 01:18
  • @AndrewHeath Could you show us what `templates/appt.php` looks like? – F21 Nov 08 '11 at 01:20
  • In that loop, you won't be able to modify the contents of the HTML unless that include file does something like `= $appt['title'] ?>`, etc. **My main point:** Your "template" would have a specific variable protocol required to make use of it. These variables need to be in the same scope as the include, which can be dangerous. You can always encapsulate this into a function. – nickb Nov 08 '11 at 01:23
  • @phpdev - updated. @nickb - I don't understand the scope downside you mention. By including it, I must have an `$appt` variable ready, this I understand. Is that all? – Drew Nov 08 '11 at 01:34
  • Typically a true templating engine wouldn't have this dependency upon a specific variable convention. Also, my point with variable scopes is that it can cause the code to be poorly maintainable, especially if you accidentally set `$appt` to something between your `include()` – nickb Nov 08 '11 at 01:41
  • You can use the function `extract` to bring `$appt['title`]` as `$title` in the current scope (See [Using PHP as Your Template Engine](http://thephppro.com/articles/pte.php) for example). – alexisdm Nov 08 '11 at 01:47
  • Is this helpful?: Include helper with it's own variable scope: http://stackoverflow.com/questions/7697389/is-include-require-with-side-effects-a-bad-practice/7697490#7697490 – hakre Nov 08 '11 at 01:49

1 Answers1

0

You may want to check out on http://mustache.github.com/ for PHP.

student
  • 213
  • 2
  • 5