【问题标题】:Symfony Twig variable not recognized (Variable does not exist)Symfony Twig 变量无法识别(变量不存在)
【发布时间】:2021-04-14 15:44:20
【问题描述】:

这在几个小时前工作得很好,我从数据库中获取数据并简单地将其显示在 Twig 的表格中。 这是我从中获取具有我的实体“Cours”的变量实例的控制器

 /**
     * @Route("/listeCours", name="liste_cours")
     */
    public function index()
    {
        $lstCours = $this->getDoctrine()
            ->getRepository('App:Cours')
            ->findAll();

        return $this->render('liste_cours/index.html.twig', ['lstCours' => $lstCours]);
    }

我将实体“Cours”的存储库中的数据保存在变量“lstCours”中,这是我稍后将在 twig 中调用的变量,

{% for cours in lstCours %}
                        <tr>
                            <td>{{ cours.nom }}</td>
                            <td>{{ cours.type }}</td>
                            <td>{{ cours.description }}</td>
                            <td>{{ cours.prix }}</td>
                            <td>
                                <button class="btn_apt btn"><a href="">Ajouter</a></button>
                            </td>
                        </tr>

编译器不断显示异常,指出变量 (lstCours) 不存在。

提前谢谢你。

【问题讨论】:

  • 您是否尝试清除缓存
  • 还是一样的异常
  • “这在几个小时前还可以正常工作” - 两者之间发生了什么变化?您可以回溯您对代码所做的任何更改吗?
  • 添加了接口之间的重定向,没有任何可能干扰该功能的东西。还有一个和这个一样的,效果很好,但是这个不行。
  • 尝试将文件转储到 twig 中。 {{ dump(lstCours) }} 看看里面有什么。

标签: php database symfony controller twig


【解决方案1】:

当你想调试你的 Twig 上下文时,你可以使用:

{{ dump(_context) }}

它将显示模板中所有可用的变量。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-27
    • 2013-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-10
    • 2017-01-04
    相关资源
    最近更新 更多