A "simple" class of templates is being developed, the problem is, how can I execute PHP code inside a string without using eval?
The following example: my template class works:
$user = 'Dave'; ob_start(); include 'index.tpl'; $content = ob_get_clean(); // String $pattern = sprintf('/%s\s*(.+?)\s*%s/s', '{{', '}}'); // replace with php tags $new_content = preg_replace($pattern, '<?php echo $1; ?>', $content); echo $new_content;
index.tpl
<html> <head></head> <body> Hello {{ $user }}! </body> </html>
I get the following result:
Hello !
I do not want to use eval, because how slow and bad it should be, is there any other way to do this? The laravel blade engine does not use eval, so it should be.
Thanks,
Joel
source share