基于PHP5的轻量级模板类/系统 [英] Lightweight PHP5 based template class/system

查看:85
本文介绍了基于PHP5的轻量级模板类/系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑将模板系统用于新项目,它只是一个很小的站点,并且不想使用smarty的开销和复杂性".我真的不喜欢模板系统会强迫您使用另一种语言,只是使设计人员(显然)更容易.

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

像这样的事情 http://www.namepros.com/code/517342-php5-template -class.html 我正在看的是它,但是它更强大,更可靠.

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.

推荐答案

PHP本身已经是模板引擎.那么,为什么不减少用模板引擎编写的模板引擎带来的开销,然后只使用PHP呢?

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>

如果您需要其他功能,请考虑使用ViewHelper,例如封装诸如添加链接名称或翻译之类的东西的小型函数,例如

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>

如果太冗长,请查看 HEREDOC和NOWDOC语法,如果这仍然不是您想要的,这里是一些模板引擎的列表:

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

或者,如果您实验性的话,请查看XHP,这是Facebook对模板引擎的扩展方法:

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

这篇关于基于PHP5的轻量级模板类/系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆