php twig输出html,php – HTML不呈现[Twig] / [Slim]

我使用twig作为模板引擎但我的html没有呈现.全部显示

HTML标记本身.

我搜索了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);

与您的代码步骤

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

您需要将所有这些放在控制器中渲染视图,也就是说,用这个代替代码.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值