2013-10-21 24 views
5

存在我已經在我的Symfony2項目如下問題:變量不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 

回答

11

它看起來像一個字符的問題。您的{{ customer }}有假冒空間char(194)
嘗試將它們移除並添加真實空間。

,當你打Alt鍵的Gr出現這個字符+ 空間(它發生在我身上很多)

提示

  • TWIG忽略空間,不會導致在有錯誤的情況下是否有真實的空間問題
  • 大多數情況下,當你看到

    變 「富」 不存在

    它意味着你有foo可變

+0

非常感謝後,這個角色!你解決了我的問題。 – Victor