2012-01-14 92 views
1

我有这个功能,但它不好。这使它很难调试我的代码:PHP模板/布局函数

fun... { 
    eval('?>' . str_replace(
     '{yield}', 
     file_get_contents('templates/' . $template . '.phtml'), 
     file_get_contents('templates/layouts/' . $layout . '.phtml') 
    )); 
} 

请让我知道更好的替代品。

谢谢。

+1

看起来像PHPBB的最佳方式:) ..什么是你特别想解决?这看起来像是一个相当重要的功能部分,并加载了你的模板部分。 – Silvertiger 2012-01-14 08:39:44

回答

1
function render($template, $layout) { 
    $cache = "wherever/you/put/the/cache/file/{$template}_{$layout}.phtml"; 

    if (!file_exists($cache)) { 
     $template = file_get_contents('templates/' . $template. '.phtml'); 
     $layout = file_get_contents('templates/layouts/' . $layout . '.phtml'); 

     $output = str_replace('{yield}', $template , $layout); 
     file_put_contents($cache, $output); 
    } else { 
     include($cache); 
    } 
} 
1

使用输出缓冲的acheiving填充模板数据

ob_start(); 
include "my/template/link.phtml"; 
$content = ob_get_contents(); 
ob_end_clean();