Beyond The Template Engine

You’re right, faster and easier, because separating logics is not at all about that. It’s about improving your code and design. The coder is happy because he doesn’t have to deal with HTML code, it’s not his job afterall. The designer is happy because he doesn’t have to deal with PHP code, business logic. You can’t seriously ask designers to fetch data from a database using “mysql_fetch_array”-like functions nor to “encrypt” their beautiful HTML codes into “echos”. It’s not a life for a designer, I think it would even kill “it”.

So let’s clean up your code to see how designer-friendly it could be :

Here’s your code rewritten following these principles :

  <select name="category">
    <option value="0">bitte auswählen</option>
<?php foreach ($categories as $row) : ?>
    <option value="<?= $row['catid'] ?>"<?= ($category == $row['catid']) ? ' selected' : '' ?>><?= $row['title'] ?></option>
<?php endforeach; ?>
  </select>

The problem is, people still think way to much in terms of PHP & HTML.

I’m rather disgusted to see how so many people fall back to template “solutions”, because they think the designer needs access to ALL of the html.

Jmmolina, your referring how a coder is happy not to deal with HTML code, and a designer doesnt want to deal with PHP code. All good & well, but that is one of the biggest contractions in terms i’ve seen a while.

Coder -> Code -> … <- HTML < - Designer

Notice a problem here? Somebody needs to be the sucker who writes the basic HTML ( tables etc ) or PHP ( loops, variables ). And in general, the Designer are the group who have little to no know how about programing. Even worse, sinds they have no know how of the variables, arrays names etc used, they can’t do much but make a site look good. Its the Coder who ends up writing most of the html for the modules/functions/etc blocks anyway.

Now, to fall back to the basics. Sinds its our job most of the time to include almost all the basic HTML anyways, we also can include something called … CSS definitions.

Now all of a sudden, that developer does NOT need to do any HTML writing, sinds the basic HTML is there ( we coders need to put it in anyway ), and everything is equip with CSS. So whats left? Of course, the basic “layout” of the site.

Now, where i work, they did a rather simple solution to that problem. The layout of the site is in the database, split up in code blocks. So, you have Row1: Your site top html. Row2: your search module etc, Row3: Start of your left page block. Row4: Module 1, Row5: Module 2, Row6: Close Html code of the left block … And your site is rendered by sequentially reading & displaying the html or module blocks.

All of a sudden, all the designer needs to do is, make a copy of the HTML output ( what is nothing more then a simple SQL Query loop, that any coder can make in 1 min time ), and a simple Admin interface to update the code blocks ( and put back the “updated” html ).

What do we see now … The designer hardly touches anymore HTML, the basic layout, and that’s it. All the rest he/she does with CSS.

Now, all that’s needed is a basic call to render the page on a request from the user. Takes only 1 simple sql call. And that’s all the overhead you end up with.

Instead of placing all the overhead on your rendering ability by splitting, you limit it to a very small fraction. And its even more secure, because at most the designers can **** up the layout themselves, and not do anything wrong to your code. Hell, with one added inner join, the query can be made to server 1000 of different pages, with so little to no overhead.

I’m amazed to read all these topics here, about one after another contraption to separate code logic from layout logic, when in most cases, there is no need for it at all thanks to css, and a bit of clever programing.

Sure, at times a designer will need to call upon a coder to add a few ‘missing’ css tags, but that’s as far as the interaction needs to go.

Use the KISS system people. Think before you implement things. Don’t fall back on templates just because everybody before you did. You save more time writing a simple admin page for the developers to alter the layout, move your modules/functions around & attach the css files to that layout ( all end up in the DB ), then almost double writing your loops, increasing your memory consumption, running replace patterns etc …

@jmmolina: Thanks for your hints.

At first I have to say I`m the designer and coder in one person. In the case other designers are involved in my projects they never have any idea of HTML.

And what about PHP 4? I have no idea how to achieve that. I really would like to see a solution.

I don´t use them because I heard that they might be switched off by the ISP in php.ini.

I don’t like it because my text editor doesn´t recognize that both parts belong together.

oerdec

Just a merge of set() and set_vars() methods.
Useful for lousy coders like me ;-).

/**
* Set a template variable.
*
* If value is not set and $name is an array, this
* sets the key-value pairs in the array to $this->vars.
*
* @param string $name name of the variable to set
* @param mixed $value the value of the variable
*
* @return void
*/
function set($name, $value = null) {
if (is_array($name) and $value = null) {
$this->vars = array_merge($this->vars, $name);
} else {
$this->vars[$name] = $value;
}
}

very good article

You’ve got “<?=$user[‘name’];?>” in your template. What if user puts HTML in there? A malicious script?

That’s why I prefer full-featured template engines, like PHPTAL or OPT2, which handle all escaping automatically (that’s the key).

Thank you for this great explanation of template classes. I have been looking for a tutorial like this forever. I am new to PHP and SQL, but this has helped me tons in my quest to implement my own template class.

One question about the user_list file. In the function, fetch_user_list, you hard code in the names, id’s and emails for the list.

However, I am trying to pull this information in from a database and can’t figure out how to populate the data array.

If I use the code below, it seems that I can’t access the data in the array because there are no identifiers associated with the sub-arrays.

while($row = mysql_fetch_array($result))
{
$arr=$row;
}

I realize this might be a stupid question, but any help would be appreciated.

Thanks,
Belize Nut

Shouldn’t you be detecting usernames for invalid characters by registration?

Sorry, to revive and old thread. I was searching around the forum and I couldn’t find a better place. I’m new to using php as a template engine. I really like the idea and want to implement a system based on Brian’s article but have a few questions.

I noticed the article was rewritten on 03, has there been any updates to the code for the new versions of php?

If you guys know of any articles or examples I would love to see them.

Thanks!

Version 3 of Smarty is out in Beta forum, it’s a complete re-write. I’ve never used any version of Smarty myself so can’t say how good it is.

Outstanding! I’ve been using XTemplate for years, but was looking to change because of a lack of activity on that project. After reading several articles and reviews, I had basically decided that I didn’t really need a full-blown, bloated template engine. This solution perfect.

Thanks again for your work!