Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5

Simple PHP Template Engine

#5
I have the new version up on Github (in beta). It's bare-bones, approximately 6.55KB, and is fully functional for basic templating applications. A mere 166 lines in whole, it is likely one of the smallest template engines in existence! Big Grin

It's also lightning fast. It compiles the templates down to pure PHP files and caches them, so there is near-zero overhead with parsing the templates and rendering them. This proved to be much more stable and much easier than the previous interpreter-based approach.

https://github.com/Darth-Apple/simple-php-templates

Still working out a few rough edges and bugs, but as of yet, it's managed to beat the 10KB goal, and it's even implemented more features than originally planned. I'm looking forward to using it for future projects and university assignments. Finna

Edit: Just like last time, we tried our minification experiment to squash it into the smallest size possible. Click the spoiler to see stats.

[spoiler]
This time, the final tally was 2,550 bytes. (3,922 bytes unminified). This was exactly 10x the size of the 255 bytes we got from the last version, but being that this was a much more advanced, fully fledged template compiler, we can call it a success. Finna

Furthermore, XZ (a 7-zip like LZMA container) got it down to a mere 996 bytes, and gzip got down to 918 bytes with a standard ZIP format. This version proved to be significantly more compressible than the last. The hefty regex probably deserves a fair portion of the credit for XZ's ability to squash the file this time. (And this is interesting, given that we minified it to total uncompressibility last time around. No compression algorithm could shrink it any further. Finna)


Code:
<?php class template_engine{public $b=array();public $l=array();private $loc;private $cachePath="templates/cache/";private $templatePath="templates/";private $enable_cache=1;public function __construct($l){$this->loc=$l;}public function set($k,$v){$this->b[$k]=$v;}public function load_lang($f){require "languages/".$this->loc."/".$f.".lang.php";$this->l=$this->l+$l;}public function parse($t){return $this->render($t,1);}public function parse_raw($t){ob_start();eval("?>".$this->compile($t,0,1)."<?php");return ob_get_clean();}public function load($t){$this->set($t,$this->parse($t));}protected function format_expression($e){return preg_replace('/\$([A-Za-z0-9_]*)/','$this->b["$1"]',$e);}public function compile($tpl,$c=1,$r=0){$d=$tpl;if(!$r){$d=file_get_contents("Styles/default/Templates/".$tpl.".html");}$d=str_replace("[@else]",'<?php else: ?>',$d);$d=str_replace("[/if]",'<?php endif; ?>',$d);$d=preg_replace('/\[@template:([a-zA-Z0-9_]+)\]/','<?php echo $this->render("$1", 1); ?>',$d);$d=preg_replace('/\[@template:@([a-zA-Z0-9_]+)\]/','<?php echo $this->render($this->b[\'$1\'], 1); ?>',$d);$d=preg_replace("/\[@([a-zA-Z0-9_]+)\]/",'<?php echo htmlspecialchars($this->b[\'$1\']); ?>',$d);$d=preg_replace('/\[\$([a-zA-Z0-9_]+)\]/','<?php echo $this->b[\'$1\']; ?>',$d);$d=preg_replace('/\[@lang:([a-zA-Z0-9_]+)\]/','<?php echo $this->l[\'$1\']; ?>',$d);$d=preg_replace_callback("#\[@loop: *(.*?) as (.*?)]((?:[^[]|\[(?!/?@loop:(.*?))|(?R))+)\[/loop]#",function($l){$i=preg_replace("#\[@loop: *(.*?) as (.*?)]((?:[^[]|\[(?!/?@loop:(.*?))|(?R))+)\[/loop]#",'<?php foreach(\$this->b[\'$1\'] as \$$2): ?> $3 <?php endforeach; ?>',$l[0]);$i=preg_replace('/\[@'.$l[2].':([a-zA-Z0-9_]+)\]/','<?php echo htmlspecialchars($'.$l[2].'[\'$1\']); ?>',$i);return preg_replace('/\[\$'.$l[2].':([a-zA-Z0-9_]+)\]/','<?php echo $'.$l[2].'[\'$1\']; ?>',$i);},$d);$d=preg_replace_callback('#\[@(if|elif|else if): *(.*?)]#',function($ex){if($ex[1]=="elif"||$ex[1]=="else if"){$ct="elseif";}else{$ct="if";}return "<?php ".$ct." (".$this->format_expression($ex[2])."): ?>";},$d);if($c){file_put_contents($this->cachePath.$template.".php",$d);}else{return $d;}}public function render($tpl,$r=0){$cF=$this->cachePath.$tpl.".php";$tF=$this->templatePath.$tpl.".html";if($this->enable_cache){if(file_exists($cF)){if(filemtime($cF)<filemtime($tF)){$this->compile($tpl);}if($r){ob_start();require($this->cachePath.$tpl.".php");return ob_get_clean();}else{require($cF);}}else{$this->compile($tpl);$this->render($tpl);}}else{eval("?>".$this->compile($tpl,0)."<?php");}}}
[/spoiler]

Reply


Messages In This Thread
Simple PHP Template Engine - by Darth-Apple - October 15th, 2020 at 10:03 PM
RE: Simple PHP Template Engine - by tc4me - October 16th, 2020 at 6:13 AM
RE: Simple PHP Template Engine - by Darth-Apple - November 25th, 2020 at 6:55 AM
RE: Simple PHP Template Engine - by Darth-Apple - January 17th, 2021 at 7:32 PM
RE: Simple PHP Template Engine - by Darth-Apple - January 19th, 2021 at 12:37 AM
RE: Simple PHP Template Engine - by tc4me - January 19th, 2021 at 6:31 AM
RE: Simple PHP Template Engine - by Darth-Apple - January 27th, 2021 at 6:49 AM
RE: Simple PHP Template Engine - by tc4me - January 27th, 2021 at 10:59 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Simple PhpBB theme (getting my feet wet) Darth-Apple 3 2,459 October 23rd, 2020 at 7:05 PM
Last Post: Megan
  Good, simple blog software? Darth-Apple 0 3,708 June 19th, 2013 at 6:23 PM
Last Post: Darth-Apple



Users browsing this thread: 1 Guest(s)

Dark/Light Theme Selector

Contact Us | Makestation | Return to Top | Lite (Archive) Mode | RSS Syndication 
Proudly powered by MyBB 1.8, © 2002-2024
Forum design by Makestation Team © 2013-2024