Created
January 14, 2016 10:10
-
-
Save lloydzhou/9e81afb1f946a1123fb7 to your computer and use it in GitHub Desktop.
template
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 t($t,$d='data'){return "extract(\$$d);".str_replace(['{{','}}','{%','%}'],['");$c(',');$c("','");','$c("'],'%}'.$t.'{%');} | |
// demo | |
$data = [ | |
'title' => 'test title', | |
'messages' => ['message1', 'message2', 'message3'] | |
]; | |
$c='print_r'; | |
// compile template and save to file | |
file_put_contents($target_file, '<?php '. t(file_get_contents('test.tpl'))); | |
include ($target_file); | |
// compile template into function. | |
$f = create_function('$d,$c', $t = t(file_get_contents('test.tpl'), 'd')); | |
$f($data, 'print_r'); |
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> | |
{% } %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment