Skip to content

Instantly share code, notes, and snippets.

@lloydzhou
Created January 14, 2016 10:10
Show Gist options
  • Save lloydzhou/9e81afb1f946a1123fb7 to your computer and use it in GitHub Desktop.
Save lloydzhou/9e81afb1f946a1123fb7 to your computer and use it in GitHub Desktop.
template
<?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');
<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