i want to create a template engine with php regex
my template engine is the following code
<?php
class tmp
{
public function assign($name,$val){
$GLOBALS[$name]=$val;
}
public function compile($buffer){
$buffer= '?> '.preg_replace('~\{\!\!\s*\$(\w+)\s*\!\!\}~', '<?php echo $GLOBALS["$1"]; ?>', $buffer);
file_put_contents('compiled.php', $buffer);
return $buffer;
}
public function run($run){
return eval($run);
}
}
?>
and my theme is the following code
{!! $bar !!}
and compiled theme is
?>
<?php echo $GLOBALS["bar"]; ?>
I access to vars with $GLOBALS but in blade the compiled code is as follows
<?php echo $bar; ?>
how blade can access to defined vars directly with their name?
I can't confirm this is how blade is doing it, though I suspect it would be, but basically extract does that exactly what you are asking:
http://php.net/manual/en/function.extract.php
It takes an array of variables and inserts directly into the symbol tables so they be accessed directly. Used in combination with get_defined_variables()
it becomes very useful.