我使用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"
}
}
}
最佳答案 几个月前我解决了同样的问题.
你需要在你的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);
与您的代码步骤
>下载:http://pear.twig-project.org/get/Twig-1.24.0.tgz
>解压缩并放置在proyect中.
>在您的文件中写下:
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
您需要将所有这些放在控制器中渲染视图,也就是说,用这个代替代码.