HTML is not rendered [Twig] / [Slim]


HTML is not rendered [Twig] / [Slim]

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

点击此处可以找到数据库中的数据

我搜索了SO,得到了许多提供解决方案的帖子,但没有一个对我有效

以下是解决方案:

  1. 使用以下代码[不工作]

    {{ detailArticle.artdesc|raw }}
     or
    {% autoescape false %}
      {{ detailArticle.artdesc }}
    {% endautoescape %}
    
  2. 使用过滤器和自动加载,如下所示[不工作]

    $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. 清除Twig缓存[我在cPanel上没有CLI,所以不确定如何执行]

    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"
    }
 }
}

几个月前我解决了同样的问题。你需要在你的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 

当你想在控制器中渲染视图时,你需要把所有这些都放在那里,也就是说,你用这个来代替你的代码。

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

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