Slim 2渲染直接HTML



我有一个正在使用Slim版本2的旧项目。我无法升级到3。

我正在尝试将树枝整合到Slim 2 中,同时还要保持旧的默认Slim2渲染器。

目前我有这个。

class TwigView extends SlimView
{
    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 SlimSlim($config);

这个想法是,当我需要渲染twig模板并使用$app->render('template.php')的所有其他模板时,我会称之为$app->view->rendertwig('file.twig')

但是,我会遇到一个错误,因为在我的rendertwig函数中$ this-> render((函数需要第一个参数的模板名称。有没有一种方法可以直接渲染Twig的结果,而无需模板文件?

我知道拥有两个模板引擎是不好的,但最终我将所有内容都切换到树枝,但我需要它作为临时解决方案,直到我可以修补所有内容。

当我检查Slim的视图对象时,它将其定义为将解释问题的渲染方法。

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 SlimView
{
    public function rendertwig($template,$data = array()){
        global $twig;
        $twigResults = $twig->render($template,array('test' => '1'));
        echo $twigResults;
    }  
}

我看到所有渲染方法所做的只是需要模板,所以我认为它可以安全地回荡Twig模板引擎的结果?这似乎从我的测试中起作用。

最新更新