Skip to content

Instantly share code, notes, and snippets.

@lloydzhou
Last active January 14, 2016 02:48
Show Gist options
  • Save lloydzhou/e9ae102d711e74b78ac0 to your computer and use it in GitHub Desktop.
Save lloydzhou/e9ae102d711e74b78ac0 to your computer and use it in GitHub Desktop.
<?php
$template = tplite();
$callback = function($s){print $s;};
$template(file_get_contents('test.tpl'), [
'title'=>'test title',
'messages' => ['test message1', 'test messages2']
], $callback);
?>
<h1>{{$title}}</h1>
{% foreach($messages as $message){ %}
<p>{{$message}}</p>
{% } %}
<?php
function tplite($tmpl) {
return create_function('$d, $c', 'extract($d);'. str_replace(['{{', '}}', '{%', '%}'], ['");$c(', ');$c("', '");', ' $c("'], '%}'. $tmpl. '{%'));
}
$t = tplite(file_get_contents('test.tpl'));
$t([
'title' => 'test title',
'messages' => ['message1', 'message2', 'message3']
], function($str){ echo $str; });
?>
<?php
function tplite(){
$_cache = [];
return function ($tmpl, $data=null, $cb=null) use (&$_cache) {
if (!isset($_cache[$key = md5($tmpl)])) $func = $_cache[$key] = create_function('$data, $c', 'extract($data);'. str_replace(['{{', '}}', '{%', '%}'], ['");$c(', ');$c("', '");', ' $c("'], '%}'. $tmpl. '{%'));
return $data && $cb ? $func($data, $cb) : $func;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment