Returning custom array in lazy builder callback I have to return a custom array from a lazy builder callback and then loop through that array in a Twig template for a custom block. Here is my code so far:
Custom block:
public function build() {

return [
  '#theme' =&gt; 'custom_theme',
  '#variables' =&gt; [
    '#lazy_builder' =&gt; [static::class . '::lazyBuilder', []],
    '#create_placeholder' =&gt; TRUE,
  ],
];
}

The callback
  public static function lazyBuilder() {

  //code above
      foreach ($vars as $var) {
        $arr[] = [
          'title' =&gt; $var-&gt;getTitle(),
          'body' =&gt; $var-&gt;body-&gt;value),
          'summary' =&gt; $var-&gt;summary-&gt;value,
        ];
      }
    }

    sleep(3); // For testing only
    return $arr; 
  }

In my twig template
{% for var in variables %}
  &lt;div&gt;{{ var.title }}&lt;/div&gt;
{% endfor %}


For each item I want to display its title, body and summary. Everything works fine if I return from the callback an array which has #markup inside, but I need to return my custom array.
If that's not possible it would be also OK if a lazy loaded the whole block not just the content inside it.
Thank you.
I have to return a custom array from a lazy builder callback

You can return multiple #markup elements nested in the render array, also other render elements like #plain_text, which is more safe to output a title containing user input:
  'title' =&gt; ['#plain_text' =&gt; $var-&gt;getTitle()],

For the body field you need a more complex render element, see How to safely render node body on a custom variable?


and then loop through that array in a Twig template

You can't loop through the array in the theme because you have set-up the variable to be lazy-built. The array doesn't exist at this point, only the placeholder. The array needs to be a proper render array which can be rendered recursively as explained above.


If that's not possible it would be also OK if a lazy loaded the whole
block not just the content inside it.

Blocks are lazy-built by default and placeholdered when meeting the auto-placeholder conditions. See https://www.drupal.org/docs/8/api/render-api/auto-placeholdering