Lightweight PHP5 based template class/system

后端 未结 6 2077
遥遥无期
遥遥无期 2020-12-16 03:08

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

相关标签:
6条回答
  • 2020-12-16 03:40

    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>
    
    0 讨论(0)
  • 2020-12-16 03:43

    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

    0 讨论(0)
  • 2020-12-16 03:45

    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)

    0 讨论(0)
  • 2020-12-16 03:51

    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/

    0 讨论(0)
  • 2020-12-16 03:54

    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
    0 讨论(0)
  • 2020-12-16 03:57

    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.

    0 讨论(0)
提交回复
热议问题