tags:

views:

466

answers:

7

Hi there.

Looking at using a template system for a new project, it's only a small site and don't want to use the overhead and 'complexity' of smarty. I don't really like template systems that force you to make use of another language just to make it easier for designers (apparently).

Something like this http://www.namepros.com/code/517342-php5-template-class.html is what Im looking at but something which is a bit more robust and proven.

+10  A: 

TWIG

I would recommend using Twig

  • extensible syntax
  • efficient
  • compiles and caches your templates to PHP classes with a very small overhead
  • sandbox mode to evaluate untrusted template code
  • unit tested
  • great documentation
  • multiple template inheritance, template blocks, automatic output-escaping

Also read Fabien Potencier's blog post, where he explains needs for a powerful and customizable template engine.

TWIG Template code

{% extends "layout.html" %}

{% block title %}
    {{ page.title|escape|title }}
{% endblock %}

{% block content %}
    Content of the page...

    {% for user in users %}
      * {{ user.name }}
    {% else %}
        No user has been found.
    {% endfor %}

{% endblock %}

{# this is a comment in twig syntax #}

Symfony Components

Also if you need additional components for web development, but you already have a defined code base, have look at Symfony Components which includes additional templating component (mentioned in XUE Can answer)

Juraj Blahunka
I fail to see how this qualifies for the OP's requirement of not having to use another language/syntax
Gordon
To be honest, I haven't read that line somehow.. shame on me.. However `symfony templating component` fully qualifies for the job
Juraj Blahunka
+1 because twig
nikic
A: 

simplest... just create class blocks like:

class MyBlock implements IHtmlRenderizable{
public function toHtml(){
   include('/the/template.phtml');
}
}

and use $this->whatever on the template.

useless
+7  A: 

PHP by itself is already a template engine. So why not cut out the overhead a template engine written in a template engine brings with it and just use PHP then?

<h1><?php echo $pageTitle ?></h1>
<div>
    <ul>
    <?php foreach($items as $item): ?>
        <li><?php echo htmlentities($item); ?></li>
    <?php endforeach; ?>
    </ul>
</div>

If you need added functionality, consider using ViewHelper, e.g. small functions that encapsulate stuff like adding links names or translating, e.g.

<table>
<?php foreach($items as $key => $item): ?>
    <tr class="<?php echo oddEven($key)?>">
        <td><?php echo productLink($item->id); ?></td>
        <td><?php echo translate($item->description); ?></td>
    </tr>
<?php endforeach; ?>
</table>

If that's too verbose, have a look at HEREDOC and NOWDOC syntax and if this is still not what you are looking for, here is a list of some template engines:

Or, if you feel experimental, have a look at XHP, Facebook's extension approach to a Template engine:

Gordon
Indeed, this ease is what im after however if the php is in one file and the template (your examples above) are in the other file. How can I get $pageTitle and the other vars you've shown above into that scope?just do includes? Feels a bit rough...
Wizzard
@Wizzard depends on your application architecture. If all your controlling code is in one file, you can just include the template at the end of that file and then the vars will be available. If you are looking for a more MVC style approach, just use a class like suggested by @useless and assign everything you want to be available to the View instance. Vars have to start with $this then of course.
Gordon
+1 For providing the most simple and direct solution. There really is no need for smarty or template engines that use str_replace
AntonioCS
+1 Having a templating engine on top of PHP (which is a template engine itself) never made a lot of sense for me
NullUserException
A: 

I believe the PHP itself is a very powerful template engine.

If you just need a very simple template engine, you can wrap a str_replace(), for example:

function template($source, array $vars, $return=false) {
    foreach ($vars as $key=>$value) {
        $source = str_replace('{'.$key.'}', $value, $source);
    }
    if ($return) {
        return $source;
    } else {
        echo $source;
    }
}

And there is a simple yet flexible template engine from symfony if you need a full featured solution.

XUE Can
+1  A: 

this is the one i've written a while ago

http://stereofrog.com/blok/makrell

it lets you define your own "template language" on the fly (or define no language at all)

PS php is not a template engine

stereofrog
A: 

Hey Wizzard,

I just released an open source project which makes this really easy. It is "Template Inheritance", inspired by Django's, and will let you inherit and override parts of a parent template from a child template. Located here:

http://phpti.com/

arshaw
A: 

I wrote Nest. It's a templating language based on the better parts of JSP, and it compiles to php code. You write your code in well-formed HTML and can easily create new tags to give you new functionality. There are some standard tags built in.

http://nest.sourceforge.net/

Looks like this:

<html xmlns:c="urn:nsttl:HTML_Template_Nest_Taglib_Standard">
  <body>
    <ul>
      <c:foreach items="posts" var="post">
        <li>${post->title}</li>
      </c:foreach>
    </ul>
</body>
tthomas48