title: How to write the rendered content into a file
body: <p>I want to get the content rendered, e.g. <code>$this-&gt;render('ProBundle:Default:view.html.twig', array('data' =&gt; $data));</code>, and write it into a file.</p>
  
  <p>I've already created a file class:</p>
  
  <pre><code>&lt;?php
  
  namespace Pro\ConvocationBundle\Entity;
  
  use Symfony\Component\HttpFoundation\File\File as SymfonyFile;
  use Symfony\Component\Validator\Constraints;
  use Doctrine\ORM\Mapping as ORM;
  
  /**
   * @ORM\Entity
   * @ORM\Table(name="file")
   */
  class File
  {
      /**
       * @ORM\Id
       * @ORM\Column(type="integer")
       * @ORM\GeneratedValue(strategy="AUTO")
       */
      private $id;
  
      /**
       * @ORM\Column
       * @Constraints\NotBlank
       */
      private $name;
  
      /**
       * @var \DateTime
       * @ORM\Column(type="datetime")
       * @Constraints\NotNull
       * @Constraints\DateTime
       */
      private $created;
  
      /**
       * @var SymfonyFile
       * @Constraints\File(maxSize=1048060)
       */
      private $filesystemFile;
  
      /**
       * @Constraints\NotNull
       * @ORM\OneToOne(targetEntity="Pro\ConvocationBundle\Entity\Convocation")
       * @ORM\JoinColumn(nullable=false)
       */
      private $convocation;
  
      function __construct(SymfonyFile $file, $name)
      {
          $this-&gt;created = new \DateTime;
          $this-&gt;setFilesystemFile($file);
          $this-&gt;name = $name;
      }
  
      function getId()
      {
          return $this-&gt;id;
      }
  
      function getName()
      {
          return $this-&gt;name;
      }
  
      function getCreated()
      {
          return $this-&gt;created;
      }
  
      function setFilesystemFile(SymfonyFile $file)
      {
          $this-&gt;filesystemFile = $file;
      }
  
      function getFilesystemFile()
      {
          return $this-&gt;filesystemFile;
      }
  }
  </code></pre>
  
  <p>and a service:</p>
  
  <pre><code>&lt;?php
  
  namespace Pro\ConvocationBundle\Service;
  
  use Frosas\Error;
  use Doctrine\ORM\Event\LifecycleEventArgs;
  use Pro\ConvocationBundle\Entity\ConvocationFile as FileEntity;
  
  class ConvocationFile
  {
      private $dir;
  
      function __construct($dir)
      {
          $this-&gt;dir = $dir;
      }
  
      function postLoad(LifecycleEventArgs $args)
      {
          if (($entity = $args-&gt;getEntity()) instanceof FileEntity) {
              $filesystemFilePath = $this-&gt;getFilesystemPath($entity);
              $filesystemFile = new \Symfony\Component\HttpFoundation\File\File($filesystemFilePath);
              $entity-&gt;setFilesystemFile($filesystemFile);
          }
      }
  
      function postPersist(LifecycleEventArgs $args)
      {
          if (($entity = $args-&gt;getEntity()) instanceof FileEntity) {
              $this-&gt;saveToFilesystem($entity);
          }
      }
  
      function postUpdate(LifecycleEventArgs $args)
      {
          if (($entity = $args-&gt;getEntity()) instanceof FileEntity) {
              if ($entity-&gt;getFilesystemFile()-&gt;getPathname() !== $this-&gt;getFilesystemPath($entity)) {
                  $this-&gt;saveToFilesystem($entity);
              }
          }
      }
  
      function postRemove(LifecycleEventArgs $args)
      {
          if (($entity = $args-&gt;getEntity()) instanceof FileEntity) {
              $ok = @unlink($entity-&gt;getFilesystemFile());
              if (! $ok) throw Error::createExceptionFromLast();
          }
      }
  
      private function saveToFilesystem(FileEntity $file)
      {
          $filesystemFile = $file-&gt;getFilesystemFile();
          if ($filesystemFile === null) throw new \InvalidArgumentException("No file given");
          if (! $filesystemFile-&gt;isFile()) throw new \InvalidArgumentException("File is not a file");
  
          $path = $this-&gt;getFilesystemPath($file);
          $filesystemFile-&gt;move(dirname($path), basename($path));
      }
  
      private function getFilesystemPath(FileEntity $file)
      {
          return "$this-&gt;dir/{$file-&gt;getId()}";
      }
  }
  </code></pre>
  
  <p>and in the configuration file:</p>
  
  <pre><code>pro_convocation.convocation_file:
      class: Pro\ConvocationBundle\Service\File
      arguments: [%kernel.root_dir%/var/files]
      tags:
          - {name: doctrine.event_listener, event: postLoad}
          - {name: doctrine.event_listener, event: postPersist}
          - {name: doctrine.event_listener, event: postUpdate}
          - {name: doctrine.event_listener, event: postRemove}
  </code></pre>
  
  <p>So I guessed I could call the service and create the file:</p>
  
  <pre><code>$newContent = $this-&gt;get('templating')-&gt;render('ProConvocationBundle:Default:view.html.twig', array('data' =&gt; $data));
  
  $this-&gt;get('pro_convocation.convocation_file')-&gt;postPersist($newContent);
  </code></pre>
  
  <p>but I'm getting this error:</p>
  
  <pre><code>Catchable Fatal Error: Argument 1 passed to Pro\ConvocationBundle\Service\File::postPersist() must be an instance of Doctrine\ORM\Event\LifecycleEventArgs, string given
  </code></pre>
  
  <p>The <code>postPersist</code> accepts an event object as an argument, and not the content to write. So I'm now a bit confused about how to create the new file. Any idea?</p>