PHP模板/布局函数



我有这个函数,但它不好。这让调试我的代码变得很困难:

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

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

谢谢。

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);
    }
}

使用输出缓冲是获得填充模板数据的最佳方式

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

最新更新