Php Slim 2直接呈现HTML
我有一个使用Slim版本2的老项目。我不能升级到3 我正在尝试将twig集成到slim 2中,同时保留旧的默认slim2渲染器。 目前我有这个Php Slim 2直接呈现HTML,php,twig,slim,slim-2,Php,Twig,Slim,Slim 2,我有一个使用Slim版本2的老项目。我不能升级到3 我正在尝试将twig集成到slim 2中,同时保留旧的默认slim2渲染器。 目前我有这个 class TwigView extends \Slim\View { public function rendertwig($template,$data = array()){ global $twig; $twigResults = $twig->render($template,array('tes
class TwigView extends \Slim\View
{
public function rendertwig($template,$data = array()){
global $twig;
$twigResults = $twig->render($template,array('test' => '1'));
$data = array_merge($this->data->all(), $data);
return $this->render($twigResults, $data);
}
}
$view = new TwigView();
$config['view'] = $view; //@JA - This command overides the default render method.
//@JA - Intialize Slim
$app = new \Slim\Slim($config);
我的想法是,当我需要呈现细枝模板时,我会称之为$app->view->rendertwig('file.twig')
,并使用$app->render('template.php')
对所有其他使用默认slim2模板制作方法的模板进行处理
但是,我得到一个错误,因为在我的rendertwig函数$this->render()中,第一个参数需要一个模板名有没有一种方法可以直接将twig的结果渲染到slim引擎中,而不需要模板文件?
我知道这是一个糟糕的形式,有两个模板引擎,但最终我会把一切都切换到细枝,但我需要作为一个临时解决方案,直到我可以修补一切
当我检查slim的view对象时,它将此定义为其渲染方法,这将解释问题
protected function render($template, $data = null)
{
$templatePathname = $this->getTemplatePathname($template);
if (!is_file($templatePathname)) {
throw new \RuntimeException("View cannot render `$template` because the template does not exist");
}
$data = array_merge($this->data->all(), (array) $data);
extract($data);
ob_start();
require $templatePathname;
return ob_get_clean();
}
我不知道这是否是一个坏的形式,但我这样做是作为一个临时的解决办法
class TwigView extends \Slim\View
{
public function rendertwig($template,$data = array()){
global $twig;
$twigResults = $twig->render($template,array('test' => '1'));
echo $twigResults;
}
}
我看到渲染方法所做的只是需要模板,所以我认为只回显来自twig模板引擎的结果是安全的?这似乎在我的测试中起了作用