Proposal for "HTML_Template"

» Metadata » Status
  • Status: Proposed
» Description
About
This library allows you to easily separate the PHP code from your HTML web pages, for a better manageability of your website.
It is necessary to have a version of PHP greater than or equal to 5.2.0 and it is also necessary to have installed the class PEAR_Exception that HTML_Template used for exception handling.

How it works
HTML_Template is a powerful template engine that works as follows:
  1. the developer writes the PHP page assigning variables, constants, etc..
  2. the designer (or the same developer) creates an html page
  3. the engine always creates a compiled file that is not recreated unless it is necessary: it recreates the file only if you upload a new version of the template file
  4. if you set the cache options, the engine will also create a cache of the template

Configuration
You must set the folder where the templates are uploaded, the template's extension and the folder where the compiled template files will be created.
You can also set the folder where the cache files are stored and you must set the duration of the cache.

Controls
The controls currently available are:
  1. Local variables: {:var}, {:array_var.foo[0].bar}, {:array_var[:other_var]}
  2. Global variables: {@get.id}, {@post.id}, {@request.id}, {@files.foo.name} (ecc), {@cookie.bar}, {@env.PATH}, {@server.PHP_SELF}, {@session.name}, {@globals.foo}
  3. Constants: {!SOME_NAME}, {!SOME_V3RY_L0NG_N4ME}
  4. Mathematic operations: {% math_exp ":number / 2" %}
  5. Set new var: {% set hello "Hello, World!" %}, {% set name :user.name %}
  6. Remove spaces: {% strip %} ... {% endstrip %}
  7. Loop 1: {% for city in :cities %} ... {:city} ... {% endfor %}
  8. Loop 2: {% for key,value in @server %} ... {:key} = {:value} ... {% endfor %}
  9. Loop 3: {% for i from 1 to 10 %} ... {:i} ... {% endfor %} // 1 2 3 ... 10
  10. Loop 4: {% for i from 10 to 1 %} ... {:i} ... {% endfor %} // 10 9 8 ... 1
  11. If conditions 1: {% if :var eq "hello" %} ... {% elif :var neq "foo" %} ... {% else %} ... {% endif %}
  12. If conditions 2: {% if some_php_function(:var) %} ... {% endif %}
  13. Don't parse code - full: {% skip %} ... some tempalte command ... {% endskip %}
  14. Don't parse code - escape: \{:name} // {:name}
  15. Var modifiers: capitalize, format, html_link, html_mailto, lower, replace, strip, strip_html, strip_javascript, strip_left, strip_right, truncate, upper
  16. Modifier example: {:var|upper}, {:var|lower|replace:" ":"_"}

...and that's it! ...until now!
» Dependencies » Links
  • PHP 5.2.0
  • PEAR
» Timeline » Changelog
  • First Draft: 2010-01-31
  • Proposal: 2010-01-31