title: Variable "name" does not exist in UsersListBundle:Default:index.html.twig at line 1 - Symfony 2
body: <p>I am getting this error using Symfony 2 while fetching all users from database:</p>
  
  <pre><code> Variable "name" does not exist in UsersListBundle:Default:index.html.twig at line 1
  500 Internal Server Error - Twig_Error_Runtime 
  </code></pre>
  
  <p>My code: <code>index.html.twig</code></p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html lang="en"&gt;
  &lt;head&gt;
  &lt;meta charset="utf-8"&gt;
  &lt;title&gt;Symfony 2 Example&lt;/title&gt;
  &lt;link href="/bundles/userslist/css/main.css" rel="stylesheet" type="text/css" media="all" /&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;div id="container"&gt;
      &lt;h1&gt;User list page&lt;/h1&gt;
      &lt;div id="body"&gt;
          &lt;p&gt;&lt;a href="/"&gt;Click here&lt;/a&gt; to back to the Homepage&lt;/p&gt;
          &lt;p&gt;&lt;a href="users/add"&gt;ADD NEW USER&lt;/a&gt;&lt;/p&gt;
          &lt;p&gt;All users in the users table using: &lt;br /&gt;
          &lt;strong&gt;$repository = $this-&gt;getDoctrine()-&gt;getRepository('UsersListBundle:Users');&lt;br /&gt;
          $users = $repository-&gt;findAll();&lt;/strong&gt;&lt;/p&gt;
          {% if users is not empty %)
              &lt;table cellpadding="4"&gt;
                  &lt;tr&gt;
                      &lt;td&gt;&lt;strong&gt;ID&lt;/strong&gt;&lt;/td&gt;
                      &lt;td&gt;&lt;strong&gt;Name&lt;/strong&gt;&lt;/td&gt;
                      &lt;td&gt;&lt;strong&gt;Email&lt;/strong&gt;&lt;/td&gt;
                      &lt;td&gt;&lt;strong&gt;Phone&lt;/strong&gt;&lt;/td&gt;
                  &lt;/tr&gt;
                  {% for user in users %}
                      &lt;tr&gt;
                          &lt;td&gt;{{ user.id }}&lt;/td&gt;
                          &lt;td&gt;{{ user.name }}&lt;/td&gt;
                          &lt;td&gt;{{ user.email }}&lt;/td&gt;
                          &lt;td&gt;{{ user.phone }}&lt;/td&gt;
                          &lt;td&gt;&lt;a href="users/edit/{{ user.id }}"&gt;Edit&lt;/a&gt;&lt;/td&gt;
                          &lt;td&gt;&lt;a href="users/delete/{{ user.id }}"&gt;Delete&lt;/a&gt;&lt;/td&gt;
                      &lt;/tr&gt;
                  {% endfor %}
              &lt;/table&gt;
          {% else %}
              &lt;p&gt;No users in the database&lt;/p&gt;
          {% endif %}
          &lt;p&gt;Get ID 1 in the example table using: &lt;br /&gt;
          &lt;strong&gt;$user = $this-&gt;getDoctrine()-&gt;getRepository('UsersListBundle:Users')-&gt;find(1);&lt;/strong&gt;&lt;/p&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Here is my controller code:</p>
  
  <pre><code>/**
       * @Route("/users")
       * @Template()
       */
      public function indexAction()
      {
          $repository = $this-&gt;getDoctrine()
              -&gt;getRepository('UsersListBundle:Users');
  
          $users = $repository-&gt;findAll();
  
          return array('users' =&gt; $users);
      }
  </code></pre>
  
  <p>I have read some articles about this error. They say something about a hidden character. This character occurs when you hit Alt Gr + Space.
  Now I dont know how to remove that character. I tried retyping same code but no luck.</p>
  
  <p>Here is my User Entity:</p>
  
  <pre><code>&lt;?php
  // src/Users/Bundle/ListBundle/Entity/Users.php
  
  namespace Users\Bundle\ListBundle\Entity;
  
  use Doctrine\ORM\Mapping as ORM;
  
  /**
   * @ORM\Entity
   * @ORM\Table(name="users")
   */
  class Users
  {
      /**
       * @ORM\Column(type="integer")
       * @ORM\Id
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      protected $id;
  
      /**
       * @ORM\Column(type="string", length=255)
       */
      protected $name;
  
      /**
       * @ORM\Column(type="string", length=200)
       */
      protected $email;
  
      /**
       * @ORM\Column(type="string", length=60)
       */
      protected $phone;
  
      /**
       * Get id
       *
       * @return integer 
       */
      public function getId()
      {
          return $this-&gt;id;
      }
  
      /**
       * Set name
       *
       * @param string $name
       * @return Users
       */
      public function setName($name)
      {
          $this-&gt;name = $name;
  
          return $this;
      }
  
      /**
       * Get name
       *
       * @return string 
       */
      public function getName()
      {
          return $this-&gt;name;
      }
  
      /**
       * Set email
       *
       * @param string $email
       * @return Users
       */
      public function setEmail($email)
      {
          $this-&gt;email = $email;
  
          return $this;
      }
  
      /**
       * Get email
       *
       * @return string 
       */
      public function getEmail()
      {
          return $this-&gt;email;
      }
  
      /**
       * Set phone
       *
       * @param string $phone
       * @return Users
       */
      public function setPhone($phone)
      {
          $this-&gt;phone = $phone;
  
          return $this;
      }
  
      /**
       * Get phone
       *
       * @return string 
       */
      public function getPhone()
      {
          return $this-&gt;phone;
      }
  }
  </code></pre>
  
  <p>Here is my UserController:</p>
  
  <pre><code>&lt;?php
  
  namespace Users\Bundle\ListBundle\Controller;
  
  use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  use Users\Bundle\ListBundle\Entity\Users;
  use Symfony\Component\HttpFoundation\Request;
  
  class UsersController extends Controller
  {
      /**
       * @Route("/users")
       * @Template()
       */
      public function indexAction()
      {
          $repository = $this-&gt;getDoctrine()
              -&gt;getRepository('UsersListBundle:Users');
  
          $users = $repository-&gt;findAll();
  
          return array('users' =&gt; $users);
      }
  
      /**
       * @Route("/users/add")
       * @Template()
       */
      public function addAction(Request $request)
      {
  
          $user= new Users();
          $user-&gt;setName('Name');
          $user-&gt;setEmail('Email');
          $user-&gt;setPhone('Phone');
  
          $form=$this-&gt;createFormBuilder($user)
              -&gt;add('name')
              -&gt;add('email')
              -&gt;add('phone')
              -&gt;getForm();
  
          if ($request-&gt;getMethod()=='POST'){
              $form-&gt;bind($request);
              if ($form-&gt;isValid()){
                  $em = $this-&gt;getDoctrine()-&gt;getManager();
                  $em-&gt;persist($user);
                  $em-&gt;flush();
                  return $this-&gt;redirect($this-&gt;generateUrl('users_list_users_index'), 301);
              }
          } else {
              return $this-&gt;render('UsersListBundle:Users:add.html.twig',array(
                  'form'=&gt;$form-&gt;createView(),
              ));
          }
      }
  
      /**
       * @Route("/users/edit/{id}", requirements={"id" = "\d+"}, defaults={"id" = 0})
       * @Template()
       */
      public function editAction($id, Request $request)
      {
          if ($id == 0){ // no user id entered
              return $this-&gt;redirect($this-&gt;generateUrl('users_list_users_index'), 301);
          }
          $user = $this-&gt;getDoctrine()
              -&gt;getRepository('UsersListBundle:Users')
              -&gt;find($id);
  
          if (!$user) {// no user in the system
              throw $this-&gt;createNotFoundException(
                  'No user found for id '.$id
              );
          }
  
          $form=$this-&gt;createFormBuilder($user)
              -&gt;add('name')
              -&gt;add('email')
              -&gt;add('phone')
              -&gt;getForm();
  
          if ($request-&gt;getMethod()=='POST'){
              $form-&gt;bind($request);
              if ($form-&gt;isValid()){
                  $em = $this-&gt;getDoctrine()-&gt;getManager();
                  $em-&gt;persist($user);
                  $em-&gt;flush();
                  return $this-&gt;redirect($this-&gt;generateUrl('users_list_users_index'), 301);
              }
          } else {
              return $this-&gt;render('UsersListBundle:Users:edit.html.twig',array(
                  'form'=&gt;$form-&gt;createView(),
              ));
          }
  
          return array('name' =&gt; $id);
      }
  
      /**
       * @Route("/users/delete/{id}", requirements={"id" = "\d+"}, defaults={"id" = 0})
       * @Template()
       */
      public function deleteAction($id)
      {
          if ($id == 0){ // no user id entered
              return $this-&gt;redirect($this-&gt;generateUrl('users_list_users_index'), 301);
          }
  
          $em = $this-&gt;getDoctrine()-&gt;getManager();
          $user = $em-&gt;getRepository('UsersListBundle:Users')-&gt;find($id);
  
          if (!$user) { // no user in the system
              throw $this-&gt;createNotFoundException(
                  'No user found for id '.$id
              );
          } else {
              $em-&gt;remove($user);
              $em-&gt;flush();
              return $this-&gt;redirect($this-&gt;generateUrl('users_list_users_index'), 301);
          }
      }
  }
  </code></pre>