HTML未呈现[Twig]/[Slim]

4
我正在使用Twig作为模板引擎,但我的HTML没有呈现。所有内容都显示为HTML标记本身。

可以通过单击此处查找来自数据库的数据

我在SO上搜索到了许多帖子,提供了解决方案,但没有一个适用于我

以下是解决方案:

  1. Use below code [not working]

    {{ detailArticle.artdesc|raw }}
    
     or
    
    {% autoescape false %}
      {{ detailArticle.artdesc }}
    {% endautoescape %}
    
  2. Use filter and autoload like below [not working]

    $app->view = new \Slim\Views\Twig();
    $app->view->setTemplatesDirectory("application/view");
    $app->view->parserOptions = array(
       'debug' => 'true',
       'auto_reload' => true,
       'autoescape' => true
    );
    $app->view->parserExtensions = array(new \Slim\Views\TwigExtension());
    
  3. Clear Twig cache [I do not have CLI on cPanel, so not sure how to do this]

    rm -rf app/cache/*  OR rm -rf app/cache/prod/twig OR app/console cache:clear --env=prod
    

对我来说没有任何解决方案有效。请指导。

数据以与上面提到的链接相同的方式显示。

我的composer.json如下:

{
 "name":"panique/mini2",
 "homepage":"https://github.com/panique/mini2",
 "license":"MIT",
 "require":{
    "php":">=5.3.0",
    "slim/slim": "~2.6",
    "slim/views": "~0.1",
    "twig/twig": "~1.16",
    "panique/pdo-debug": "0.2",
    "panique/php-sass": "~1.0",
    "matthiasmullie/minify": "~1.3"
 },
"autoload":{
    "psr-4":{
        "Mini\\": "Mini"
    }
 }
}

你尝试过使用verbatim标签吗? - Matteo
如何使用这个... - Gags
嗨@gags,我在我的答案中发布了一个示例。让我知道。 - Matteo
抱歉 @gags,这不是你的情况... - Matteo
2个回答

2
我几个月前也解决了同样的问题。 你需要在你的php代码中写入以下内容:
$escaper = new Twig_Extension_Escaper(false);
$app->view->addExtension($escaper);

我的代码

require_once 'path_to_Twig/Autoloader.php';
Twig_Autoloader::register();

$loader = new Twig_Loader_Filesystem('path_to_views/');
$twig = new Twig_Environment($loader, array());

$escaper = new Twig_Extension_Escaper(false);
$twig->addExtension($escaper);

echo $twig->render($view_name, $data_to_view);

使用您的代码的步骤

  1. 下载:http://pear.twig-project.org/get/Twig-1.24.0.tgz
  2. 解压并放置在您的项目中。
  3. 将以下内容写入您的文件:
 require 'vendor/autoload.php'; 

 // Initialize Slim (the router/micro framework used) 
 $app = new \Slim\Slim(array( 
 'mode' => 'production' 
 )); 

 require_once 'path_to_Twig/Autoloader.php'; //Substitute the Twig path, the path to the uncompress files in the project 
 Twig_Autoloader::register(); 

 $loader = new Twig_Loader_Filesystem('path_to_views/'); //Substitute with the path with the html files 
 $twig = new Twig_Environment($loader, array()); 

 $escaper = new Twig_Extension_Escaper(false); 
 $twig->addExtension($escaper); 

 echo $twig->render($view_name, $data_to_view); //First the name of the view html file, data that you want pass to the view in one array 

当你想要在控制器中呈现视图时,你需要将所有这些都放入其中,也就是说,你需要用这个代码替换你的代码。


Slim\Views\Twig::addExtension() 方法未定义是错误。 - Gags
但是您已经创建了twig对象,addExtension是一个方法。很奇怪,您可以创建对象但无法访问该方法。 - Miguel Jiménez
Twig_Extension_Escaper 可以在哪里找到? - Gags
我在解决方案中写了所有的代码,我将这段代码用于所有项目。如果你在使用Slimframework时使用命名空间,请尝试直接使用Twig,就像我的代码一样。 - Miguel Jiménez
让我们在聊天中继续这个讨论 - Miguel Jiménez
显示剩余7条评论

0

遇到了同样的问题,并通过添加Twig_Extnesion_Escaper解决了它:

$v = new \Slim\Views\Twig(__DIR__ . '/../../templates', [
    'cache' => __DIR__ . '/../../templates/twigcache'
]);
$v->addExtension(new Twig_Extension_Escaper());

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接