PHP Classes

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    
Ratings Unique User Downloads Download Rankings
Not yet rated by the usersTotal: 106 This week: 1All time: 9,676 This week: 560Up
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)

Picture of David Tamas
  Performance   Level  
Name: David Tamas <contact>
Classes: 5 packages by
Country: Hungary Hungary
Age: 37
All time rank: 311028 in Hungary Hungary
Week rank: 106 Up1 in Hungary Hungary Up
Innovation award
Innovation award
Nominee: 2x

Winner: 1x

Example

<?php
function get_microtime()
{
   
$mtime = microtime();
   
$mtime = explode(" ", $mtime);
   
$mtime = (double)($mtime[1]) + (double)($mtime[0]);
    return (
$mtime);
}
$time = get_microtime();
require_once
'lib/class.template.php';

$tpl = new gTemplate();
$tpl->left_delimiter = '<{';
$tpl->right_delimiter = '}>';
$tpl->force_compile = false;

class
testclass {
    public
$v1 = 'variable 1';
    public
$true = true;
    public
$false = false;

    function
f1() {
        return
'Result of function f1';
    }
}

$tpl->assign('Name', 'G-Template Engine');
$tpl->assign('test_name', 'Dávid Tamás');
$tpl->assign('test_number', 4);
$tpl->assign('test_array', array('first'=>'First item', 'second'=>'Second item', '3'=>'Third item', 'Dávid Tamás'=>'my name is Dávid Tamás', '4'=>array('subarray'=>'Great', 'testing'=>array('3'=>'subtesting')), 'foo'=>array('bar'=>'this is foobar')));
$tpl->assign('test_obj', new testclass());

$tpl->assign('test_true', true);
$tpl->assign('test_false',false);
$tpl->assign('valami', '');

$tpl->assign("FirstName",array("John","Mary","James","Henry", 'Tamas', 'G'));
$tpl->assign("LastName",array("Doe","Smith","Johnson","Case", 'David', 'Lex'));

$tpl->assign("contacts", array(
                                array(
"phone" => "555-1111", "fax" => "666-1111", "cell" => "760-1111"),
                                array(
"phone" => "555-2222", "fax" => "666-2222", "cell" => "760-2222"),
                                array(
"phone" => "555-3333", "fax" => "666-3333", "cell" => "760-3333"),
                                array(
"phone" => "555-4444", "fax" => "666-4444", "cell" => "760-4444"),
                                array(
"phone" => "555-5555", "fax" => "666-5555", "cell" => "760-5555"),
                                array(
"phone" => "555-6666", "fax" => "666-6666", "cell" => "760-6666"),
                            ));

//$tpl->display('test.tpl');
//exit;


$tpl->assign('simpleSelect', array('6'=>'Item 1', '8'=>'Item 2', '10'=>'Item 3', '12'=>'Item 4'));
$tpl->assign('grouppedSelect', array('Group 1'=>array('6'=>'Item 1', '8'=>'Item 2', '10'=>'Item 3', '12'=>'Item 4'), 'Group 2'=>array('5'=>'Item 1', '7'=>'Item 2', '9'=>'Item 3', '11'=>'Item 4')));


$tpl->assign('glex', '<?php echo "xx"; ?>');
$tpl->assign('data', 'ingatlanok.html');


$tpl->assign('glexobj', new testclass());


$tpl->assign("Class",array(array("A","B","C","D"), array("E", "F", "G", "H"),
      array(
"I", "J", "K", "L"), array("M", "N", "O", "P")));

$tpl->assign("option_values", array("NY","NE","KS","IA","OK","TX"));
$tpl->assign("option_output", array("New York","Nebraska","Kansas","Iowa","Oklahoma","Texas"));
$tpl->assign("option_selected", "NE");

$tpl->assign('header', 'My Post Comments');

$comments = array(array( 'name'=>'Joe', 'body'=>'Thanks for this post!' ),
            array(
'name'=>'Sam', 'body'=>'Thanks for this post!' ),
            array(
'name'=>'Heather', 'body'=>'Thanks for this post!' ),
            array(
'name'=>'Kathy', 'body'=>'Thanks for this post!' ),
            array(
'name'=>'George', 'body'=>'Thanks for this post!' )
            );

$tpl->assign('comments', $comments);

    for(
$x = 0; $x < 50; $x++) {
       
$tpl->assign('foo_'.$x, 'bar_'.$x);
    }

    for(
$x = 0; $x < 50; $x++) {
       
$foo[] = 'bar_'.$x;
    }

   
$tpl->assign('foo',$foo);



$tpl->display('index.tpl');
$runTime = get_microtime()-$time;
error_log($_GET['s'].';'.$runTime.';'.memory_get_usage(true).';'.memory_get_peak_usage(true)."\n", 3, __FILE__.'.csv');


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:106
This week:1
All time:9,676
This week:560Up