我使用twig作为模板引擎,但我的html没有渲染。全部显示HTML标签本身。
Data from Database can be found by clicking here
我搜索了SO并获得了许多提供解决方案的帖子,但没有一个对我有用
以下是解决方案:
使用以下代码[无效]
{{ detailArticle.artdesc|raw }}
or
{% autoescape false %}
{{ detailArticle.artdesc }}
{% endautoescape %}
使用过滤器和自动加载功能,如下面的[无法正常工作]
$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());
清除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"
}
}
}
答案 0 :(得分:2)
$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);
使用代码的步骤
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
你需要把所有这些都放在控制器中渲染视图,也就是说,用这个代替你的代码。
答案 1 :(得分:0)
进入同一问题并通过添加Twig_Extnesion_Escaper解决它:
$v = new \Slim\Views\Twig(__DIR__ . '/../../templates', [
'cache' => __DIR__ . '/../../templates/twigcache'
]);
$v->addExtension(new Twig_Extension_Escaper());