【问题标题】:Variable does not exist in symfony2symfony2 中不存在变量
【发布时间】:2013-10-21 12:58:07
【问题描述】:

我的 symfony2 项目有以下问题:

这是我的控制器的代码

public function showCustomerAction($id) {
    // retrieve the customer from the database
    $em = $this->getDoctrine()->getManager();
    $customer = $em->getRepository('VictorIoSiteBundle:Customer')->find($id);

    //throw new \Exception($customer);
    return $this->render('VictorIoSiteBundle:Site:viewCustomer.html.twig', array('customer' => $customer));
}

还有我的树枝视图的代码(很简单):

{% if customer is defined %}
    <h3>Customer: {{ customer }} </h3>
{% endif %}

最后是我的 routing.yml

victor_io_site_show_customer:
pattern: /show-customer/{id}
defaults: { _controller: VictorIoSiteBundle:Site:showCustomer }
requirements:
    id: \d+

现在我继续

http://localhost/Garage/web/app_dev.php/show-customer/46

我收到以下错误:

Variable " customer " does not exist in VictorIoSiteBundle:Site:viewCustomer.html.twig at line 2
500 Internal Server Error - Twig_Error_Runtime

【问题讨论】:

    标签: php symfony twig


    【解决方案1】:

    看起来是字符问题。您的{{ customer }} 中有假空格char(194)
    尝试删除它们并添加一个真实的空间。

    当您点击 Alt Gr + Space 时会出现此字符(我经常遇到这种情况)

    提示

    • TWIG 会忽略空格,如果存在实际空格问题,也不会导致错误
    • 大多数情况下,当你看到

      变量“foo”不存在

      表示foo变量后面有这个字符

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-24
      • 2013-03-02
      • 2014-09-28
      • 2012-08-17
      • 2011-12-04
      相关资源
      最近更新 更多