January 19th, 2021 at 12:37 AM
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!
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.
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.
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. )
[/spoiler]
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.
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.
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. )
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");}}}