title: What is more effective: filling variables with twig filters or passing them through the controller?
body: <p>I've recently been using my own custom made twig filters. I've also been looking at ways to improve my performance.</p>
  
  <p>I struggle in being able to distinguish when I should build a function in a Service, that then gets used by a Controller or if i should make it a twig filter instead. I am wondering which one executes faster or if there is any difference at all? To clarify my question I want to show how I fill the variable photos with both.</p>
  
  <p>The twig template will look like this. It will just make image elements with the array of photos.</p>
  
  <p><strong>/content/photos.html.twig</strong></p>
  
  <pre><code>    {% for photo in photos %}
      &lt;img class="lozad card-img read-more-card-item-img" data-src="{{ photo }}"&gt;  
      {% endfor %}
  </code></pre>
  
  <p>Method 1: Pass the photos variable in a controller.
  The controller would probably use a service called PhotoService and call the getReadMore function, while passing this to the twig template:</p>
  
  <pre><code> function controllerAction(){
             $response = $this-&gt;render(
                  '/content/photos.html.twig',
                   array(
                      'photos' =&gt; $photoService-&gt;getReadMorePhotos($posts),
                      'loadMoreUrl' =&gt; $url,
                      'limit' =&gt; $limit
                  )
      }
  </code></pre>
  
  <p>Method 2: Use a custom twig filter instead:</p>
  
  <pre><code>public function getFilters()
   {
  return array (new \Twig\TwigFilter('readMorePhotos', array($this, 'getReadMorePhotos')));
  }
  
   //Twig filter
      public function getReadMorePhotos($posts)
      {
  ...
          $photos = [];
          foreach ($posts as $post) {
               $image = $this-&gt;getAbsoluteThumbsPath($post-&gt;getImage());
               if ($image != null &amp;&amp; $image != "" &amp;&amp; strpos($image, '/img/posts/default-post.png') === false) {
                    $gallery[] = $image;
               }
          }
  ...
          return $photos;
      }
  </code></pre>
  
  <p>In the twig template the photos variable will be filled like this:</p>
  
  <pre><code>{{set photos = posts|readMorePhotos}}
  </code></pre>
  
  <p>Is there any difference in both methods?</p>