title: Symfony2 Twig extension: Creating form
body: <p>I wanted to have a contact-form-block that i can reuse on different pages and templates. So i decided to write a Twig extension. The problem is that i cant access the createFormBuilder() function. The second problem will be then that i cant access the request object for validation. My current code looks like this:</p>
  
  <pre><code>&lt;?php
  
  namespace Name\NameBundle\Controller;
  
  use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  use Symfony\Component\HttpFoundation\Request;
  
  class ContactExtension extends \Twig_Extension
  {
      function getName() {
          return 'contact_extension';
      }
  
      function getFunctions() {
          return array(
             'contactform' =&gt; new \Twig_Function_Method($this, 'contactform'),
          );
      }
  
      function contactform() {
          $form = $this-&gt;createFormBuilder()
             -&gt;add('Name', 'text')
             -&gt;add('Message', 'textarea')
             -&gt;add('Send', 'submit')
             -&gt;getForm();
  
          return $this-&gt;render('NameBundle:forms:contactform.html.twig', array(
              'form' =&gt; $form-&gt;createView(),
      }
  }
  </code></pre>
  
  <p>But i get error "Call to undefined method createFormBuilder()"... </p>
  
  <p>Also i will get error if i change the function to <code>function contactform(Request $request) { ... }</code></p>
  
  <p>What do i need to add to use this function an object? Or maybe the twig extension is the completely wrong approach?</p>