PHP Classes
elePHPant
Icontem

g-template-php: Process and render templates generating PHP code

Recommend this page to a friend!
  Info   View files Example   View files View files (63)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2018-11-22 (1 month ago) RSS 2.0 feedNot yet rated by the usersTotal: 74 All time: 9,267 This week: 301Up
Version License PHP version Categories
g-template-php 1.0.0BSD License5PHP 5, Templates, Code Generation
Description Author

This package can process and render templates generating PHP code.

It can take a template file with insertion marks and processes it to generate scripts of PHP code that when executed they will replace the template marks by the parameter values.

The package comes with many plugins that extend the functionality of the template engine to provide additional features besides those that come built-in the core template engine.


Ultrafast and light compiling template engine for PHP+Zend OpCache (based on TemplateLite and Smarty)

  Performance   Level  
Name: David Tamas <contact>
Classes: 4 packages by
Country: Hungary Hungary
Innovation award
Innovation award
Nominee: 1x

Details

G-Template Engine

Ultrafast and light compiling template engine for PHP5 + Zend OpCache

The sources are based on TemplateLite (http://templatelite.sourceforge.net/) and Smarty (http://www.smarty.net/)

Basically G-Template Engine wants to be a bare minimal smarty alternative. This is the first public techpreview so use it on your own risk!

Requirements

  • PHP 5.5
  • Zend OpCache 7.0.4-dev

Main concepts

In the first words i need to say sorry for my bad englis :( I think Smarty is the best template engine for PHP but sometimes it's too slow or eats a lot of memory. Some years ago I found the TemplateLite engine. It is much faster and it doesn't need a lot of memory. But TemplateLite is very old, unsupported and Buggy.

In the first days I just fixed some bugs and cleaned the codes in the TemplateLite sources. After that I wanted to know how much faster and "lighter" TemplateLite than Smarty2 and Smarty3. So I've created some test templates with foreach, section, config file, includes and a lot of assigns from PHP. Because Smarty3 is backward compatible with Smarty2 and TemplateLite is based on Smarty2 the three template engine can use the same tpl files.

After some tests I've noticed that the main bottleneck is the slow IO and the slow file operations. If I move the template resources to memcache or something else I need to give up the power of OpCaches.

The builtin OpCache do some filesystem caching and OptIns but it wasn't enough for me :)

In the latest 7.0.4 version I found the solution: the opcache_is_script_cached() and opcache_compile_file() functions

Smarty do a lot of file_exists and filemtime() queries to the filesystem to validate the compiled resource against the tpl file. So If you fetch a compiled template the Zend OpCaches saves the binary opcodes into the memory. If you modify the compiled resource file the OpCache invalidates the currently cached opcode. With the opcache_is_script_cached() function I can make a function that tells me that the compiled resource file is modified since the last use or not. But I need to compare the tpl file against the compiled resource. So with opcache_compile_file() I can move the tpl files to the OpCache and if the tpl file modified the OpCache invalidates the cached one. So If I check the tpl file with the opcache_is_script_cached() function and it gives me a big false I check the filemtimes and if needed recompile the tpl file into php and with the opcache_compile_file function i can move the tpl file into the cache again.

In the TemplateLite sources I replaced the filemtime based comparsions with this OpCache based hack and TemplateLite gives me a 100% speed boost! If I use a lot of concurent connections in the ab (apache benchmark) smarty gets extremly slower but with this hack isnt :)

in this repo you can find my TemplateLite fork patched with this hack.

  Files folder image Files  
File Role Description
Files folder imagelib (3 files, 2 directories)
Files folder imagetemplates (9 files)
Files folder imagetemplates_config (1 file)
Accessible without login Plain text file demo.php Example Example
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  lib  
File Role Description
Files folder imageinternal (16 files)
Files folder imageplugins (31 files)
  Plain text file class.compiler.php Class Class source
  Plain text file class.config.php Class Class source
  Plain text file class.template.php Class Class source

  Files folder image Files  /  lib  /  internal  
File Role Description
  Accessible without login Plain text file compile.compile_config.php Example Example script
  Accessible without login Plain text file compile.compile_custom_block.php Example Example script
  Accessible without login Plain text file compile.compile_custom_function.php Example Example script
  Accessible without login Plain text file compile.compile_if.php Example Example script
  Accessible without login Plain text file compile.foreach_start.php Example Example script
  Accessible without login Plain text file compile.generate_c...er_debug_output.php Aux. Auxiliary script
  Accessible without login Plain text file compile.include.php Example Example script
  Accessible without login Plain text file compile.inline.php Example Example script
  Accessible without login Plain text file compile.parse_is_expr.php Example Example script
  Accessible without login Plain text file compile.section_start.php Example Example script
  Accessible without login Plain text file debug.beauty_errors.php Example Example script
  Accessible without login Plain text file debug.tpl Data Auxiliary data
  Accessible without login Plain text file template.build_dir.php Example Example script
  Accessible without login Plain text file template.config_loader.php Example Example script
  Accessible without login Plain text file template.destroy_dir.php Example Example script
  Accessible without login Plain text file template.fetch_compile_include.php Example Example script

  Files folder image Files  /  lib  /  plugins  
File Role Description
  Accessible without login Plain text file block.capture.php Example Example script
  Accessible without login Plain text file block.strip.php Aux. Auxiliary script
  Accessible without login Plain text file compiler.debug.php Aux. Auxiliary script
  Accessible without login Plain text file function.counter.php Example Example script
  Accessible without login Plain text file function.cycle.php Example Example script
  Accessible without login Plain text file function.html_options.php Example Example script
  Accessible without login Plain text file function.html_select_date.php Example Example script
  Accessible without login Plain text file function.html_select_time.php Example Example script
  Accessible without login Plain text file function.mailto.php Example Example script
  Accessible without login Plain text file function.math.php Example Example script
  Accessible without login Plain text file inlinemodifier.capitalize.php Aux. Auxiliary script
  Accessible without login Plain text file inlinemodifier.cat.php Example Example script
  Accessible without login Plain text file inlinemodifier.default.php Example Example script
  Accessible without login Plain text file inlinemodifier.lower.php Aux. Auxiliary script
  Accessible without login Plain text file inlinemodifier.strip.php Aux. Auxiliary script
  Accessible without login Plain text file inlinemodifier.upper.php Aux. Auxiliary script
  Accessible without login Plain text file modifier.count_characters.php Aux. Auxiliary script
  Accessible without login Plain text file modifier.count_paragraphs.php Aux. Auxiliary script
  Accessible without login Plain text file modifier.count_sentences.php Aux. Auxiliary script
  Accessible without login Plain text file modifier.count_words.php Aux. Auxiliary script
  Accessible without login Plain text file modifier.date_format.php Aux. Auxiliary script
  Accessible without login Plain text file modifier.debug_print_var.php Aux. Auxiliary script
  Accessible without login Plain text file modifier.escape.php Aux. Auxiliary script
  Accessible without login Plain text file modifier.indent.php Aux. Auxiliary script
  Accessible without login Plain text file modifier.regex_replace.php Aux. Auxiliary script
  Accessible without login Plain text file modifier.replace.php Aux. Auxiliary script
  Accessible without login Plain text file modifier.spacify.php Aux. Auxiliary script
  Accessible without login Plain text file modifier.string_format.php Aux. Auxiliary script
  Accessible without login Plain text file modifier.truncate.php Aux. Auxiliary script
  Accessible without login Plain text file shared.escape_chars.php Aux. Auxiliary script
  Accessible without login Plain text file shared.make_timestamp.php Aux. Auxiliary script

  Files folder image Files  /  templates  
File Role Description
  Accessible without login Plain text file footer.tpl Data Auxiliary data
  Accessible without login Plain text file header.tpl Data Auxiliary data
  Accessible without login Plain text file index.tpl Data Auxiliary data
  Accessible without login Plain text file test.conf Data Auxiliary data
  Accessible without login Plain text file test.html_options.tpl Data Auxiliary data
  Accessible without login Plain text file test.if.tpl Data Auxiliary data
  Accessible without login Plain text file test.printout.tpl Data Auxiliary data
  Accessible without login Plain text file test.section.tpl Data Auxiliary data
  Accessible without login Plain text file test.tpl Data Auxiliary data

  Files folder image Files  /  templates_config  
File Role Description
  Accessible without login Plain text file test.conf Data Auxiliary data

 Version Control Unique User Downloads Download Rankings  
 100%
Total:74
This week:0
All time:9,267
This week:301Up