I basically have a template system, it reads the template file and if it has {$test}, I want it to print the actual variable $test instead of {$test}.
So how here it works in my system:
I file_get_contents($template);
then I use preg_match_all
with the following regexp: /{\$(.*?)}/
Now, when it finds the {$variable}
in the text file, how to make it post the actual variable value? Should I use eval()
?
Here is a snippet of my code:
public function ParseTemplate()
{
// Get the file contents.
$content = file_get_contents("index.tmp");
// Check for variables within this template file.
preg_match_all('/{\$(.*?)}/', $content, $matches);
// Found matches.
if(count($matches) != 0)
{
foreach ($matches[1] as $match => $variable) {
eval("$name = {\$variable}");
$content = str_replace($name, $name, $content);
}
}
// Output the final result.
echo $content;
}
index.tmp
The variable result is: {$test}
index.php
$test = "This is a test";
ParseTemplate();
I am a bit new to eval
sooo yeah, it just prints The variable result is: {$test}
instead of The variable result is: This is a test
If you didn't get my point then just tell me in a comment and I'll try to explain better, sleepy :D