Last active
January 14, 2016 02:48
-
-
Save lloydzhou/e9ae102d711e74b78ac0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$template = tplite(); | |
$callback = function($s){print $s;}; | |
$template(file_get_contents('test.tpl'), [ | |
'title'=>'test title', | |
'messages' => ['test message1', 'test messages2'] | |
], $callback); | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<h1>{{$title}}</h1> | |
{% foreach($messages as $message){ %} | |
<p>{{$message}}</p> | |
{% } %} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; }); | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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