Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP templates - with PHP [closed]

Tags:

What's the most elegant templating (preferably in pure PHP!) solution you've seen?

Specifically i'm interested in handling:

  1. Detecting in a repeating block whether it's the first or last element
  2. Easy handling of odd/even cases, like a zebra striped table, or similar
  3. Other modulos logic, where you'd do something every n'th time.

I'm looking for something that makes this less of a pain:

<?php
$persons = array('John', 'Jack', 'Jill', 'Jason');
?>

<?php $i = 0; ?>
<?php if (isset($persons)): ?>
<ul>
<?php foreach ($persons as $name): ?>
    <li class="<?= ($i++ % 2 === 0) ? 'odd' : 'even' ?>"><?= $name ?></li>
<?php endforeach ?>
</ul>
<?php endif ?>

Does it really take the mess above to create something like this below?

<ul>
    <li class="odd">John</li>
    <li class="even">Jack</li>
    <li class="odd">Jill</li>
    <li class="even">Jason</li>
</ul>

Is it only me that find the above near hideous?

All those starting and closing of php-tags makes me cringe.

like image 859
David Avatar asked Oct 02 '08 18:10

David


People also ask

How to use PHP templates in block themes?

First, install and activate the theme and open the theme files in your code editor. Next, rename search. html to search. php and move the file from the templates folder to the themes root folder.

How do you use templates in PHP?

The simplest form of a template is something like the following code: The page holds the structure, but none of the actual contents. The contents are stored in PHP variables, which are stored in another file, called (in this example) vars. php.

Is PHP a templating language?

PHP is not a template engine, but a language that can be used to write templates, or template engines. A template engine is not just a language, but also the programming API that allows the scripts to locate, organize templates or assign the data from the script to them.


2 Answers

You don't need to open the tags more than once. You can also make a function out of it if you do the same thing multiple times:

<?php
function makeul($items, $classes) {
  $c = count($classes);
  $out = "";

  if (isset($items) && count($items) > 0) {
    $out = "<ul>\n";
    foreach ($items as $item) {
      $out .= "\t<li class=\"" . $classes[$i++%$c] . "\">$item</li>\n";
    }
    $out .= "</ul>\n";
  }
  return $out;
}
?>

other page content

<?php
$persons = array('John', 'Jack', 'Jill', 'Jason');
$classes = array('odd', 'even');
print makeul($persons, $classes);
?>

Also, if you don't mind using Javascript, Jquery makes things done mod 2 pretty easy (e.g., for zebra striping a table):

$("tr:odd").addClass("odd");
$("tr:even").addClass("even");
like image 163
Randy Avatar answered Oct 12 '22 01:10

Randy


Tiny But Strong

www.tinybutstrong.com

It doesn't make the smarty mistake of embedding another macro language in the page, but does allow you to handle every practical web display issue I've ever thrown at it. In particular the above odd/even constructs are a doddle. For something like your code selecting from a database table

In the PHP file

$TBS->MergeBlock('blk1',$sqlconnect, "SELECT name from people ");

And in the HTML file

<ul>
    <li class="odd">[blk.name;block=ul]</li>
    <li class="even">[blk.name;block=ul]</li>
</ul>

And that's it. Notice that the HTML is completely Dreamweaver compatible. Furthermore if I wanted to make that alternate over three line styles all I'd need to do is add the extra line, maybe with different classes, so

<ul>
    <li class="linestyle1">[blk.name;block=ul]</li>
    <li class="linestyle2">[blk.name;block=ul]</li>
    <li class="linestyle3">[blk.name;block=ul]</li>
</ul>
like image 45
Cruachan Avatar answered Oct 12 '22 00:10

Cruachan