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 开发者_StackOverflow社区something which is a bit more robust and proven.
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)
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:
- http://www.webresourcesdepot.com/19-promising-php-template-engines/
- http://en.wikipedia.org/wiki/Web_template_system#Server-side_systems
Or, if you feel experimental, have a look at XHP, Facebook's extension approach to a Template engine:
- http://www.facebook.com/notes/facebook-engineering/xhp-a-new-way-to-write-php/294003943919
- http://toys.lerdorf.com/archives/54-A-quick-look-at-XHP.html
- https://github.com/hhvm/xhp-lib
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://arshaw.com/phpti/
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>
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.
simplest... just create class blocks like:
class MyBlock implements IHtmlRenderizable{
private $_vars = array();
public function addVar($name, $value) {
$this->_vars[$name] = $value; return $this;
}
public function toHtml(){
extract($this->_vars);
include('/the/template.phtml');
}
}
and use $this->whatever on the template. or use:
$block->addVar('myvar', 'myvalue')->toHtml();
and on the template you can access it with $myvar
Look at the absint package at phpclasses.org. It is very small and provides a trait, a class and a function to choose from. Freeware. Or just use the global function as a replacement for vsprintf():
function absintf($string, array $vars) {
$callback = function ($matches) use ($vars) {
$match = $matches[1];
return $vars[$match];
};
return preg_replace_callback("/\{([\w\d]+)\}/", $callback, $string);
};
精彩评论