首页 > 解决方案 > 树枝模板返回 html 字符

问题描述

我与树枝模板的关系有问题。

它返回原始 html 字符而不是 html 标记。

<?php
/* HomepageController.php */

namespace App\Controller;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;

class HomepageController extends AbstractController
{
    /**
     * @Route("/homepage/")
     */
    public function homepage()
    {
        return $this->render('homepage/homepage.html.twig', [
            'title' => 'this will be title',
        ]);
    }
}


homepage.html.twig

<h1>this should show html content</h1>
  <p>{{ title }} </p>

浏览器输出

<h1>this should show html content</h1>
  <p>this will be title </p>

树枝.yaml

twig:
    default_path: '%kernel.project_dir%/templates'
    debug: '%kernel.debug%'
    strict_variables: '%kernel.debug%'
    exception_controller: null

你知道我要设置的配置是什么吗

标签: phphtmlsymfonytwig

解决方案


这是因为文件中设置了优先级。就像json一样。添加了html并且它起作用了。

fos.rest.yaml

format_listener:
rules:
  - {
      path: "^/",
      priorities: ["json","html"],
      fallback_format: json,
      prefer_extension: false,
    }

推荐阅读