Switch from eval() to include() in template class. APC caches includes nowadays so caching is not necessary any more.

This commit is contained in:
Alex Cabal 2019-04-05 18:40:48 -05:00
parent 106a156f5e
commit 530cad6cc8

View file

@ -3,25 +3,15 @@ use function Safe\file_get_contents;
use function Safe\ob_end_clean; use function Safe\ob_end_clean;
class Template{ class Template{
protected static $Cache = [];
protected static function Get(string $templateName, array $arguments = []): string{ protected static function Get(string $templateName, array $arguments = []): string{
// Expand the passed variables // Expand the passed variables to make them available to the included template.
// Use these funny names so that we can use 'name' and 'value' as template variables // We use these funny names so that we can use 'name' and 'value' as template variables if we want to.
foreach($arguments as $innerName => $innerValue){ foreach($arguments as $innerName => $innerValue){
$$innerName = $innerValue; $$innerName = $innerValue;
} }
if(array_key_exists($templateName, self::$Cache)){
$fileContents = self::$Cache[$templateName];
}
else{
$fileContents = file_get_contents(TEMPLATES_PATH . '/' . $templateName . '.php');
self::$Cache[$templateName] = $fileContents;
}
ob_start(); ob_start();
eval(' ?>' . $fileContents . '<? '); include(TEMPLATES_PATH . '/' . $templateName . '.php');
$contents = ob_get_contents() ?: ''; $contents = ob_get_contents() ?: '';
ob_end_clean(); ob_end_clean();