HTML 未渲染 [Twig] / [Slim]
HTML is not rendered [Twig] / [Slim]
我正在使用 twig 作为模板引擎,但我的 html 没有渲染。全部显示为 HTML 标签本身。
Data from Database can be found by clicking here
我搜索了 SO 并得到了很多提供解决方案的帖子,但 none 对我有用
以下是解决方案:
使用下面的代码[不工作]
{{ 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
None 的解决方案对我有用。请指导。
数据的显示方式与您在上述 link 中看到的方式相同。
我的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
- 解压缩并放入您的项目中。
- 在你的文件中写下:
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());
我正在使用 twig 作为模板引擎,但我的 html 没有渲染。全部显示为 HTML 标签本身。
Data from Database can be found by clicking here
我搜索了 SO 并得到了很多提供解决方案的帖子,但 none 对我有用
以下是解决方案:
使用下面的代码[不工作]
{{ 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
None 的解决方案对我有用。请指导。
数据的显示方式与您在上述 link 中看到的方式相同。
我的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
- 解压缩并放入您的项目中。
- 在你的文件中写下:
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());