Unverified Commit 3ec578fa authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3546376 by catch, godotislate, berdir, grimreaper, alexpott, nod_: Use...

Issue #3546376 by catch, godotislate, berdir, grimreaper, alexpott, nod_: Use the 'yield' option instead of output buffering for twig rendering to support async rendering
parent 34fa5918
Loading
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ parameters:
    debug: false
    auto_reload: null
    cache: true
    use_yield: true
    allowed_file_extensions:
      - css
      - html
+1 −0
Original line number Diff line number Diff line
@@ -75,6 +75,7 @@ public function __construct($root, CacheBackendInterface $cache, $twig_extension
      'cache' => TRUE,
      'debug' => FALSE,
      'auto_reload' => NULL,
      'use_yield' => TRUE,
    ];
    // Ensure autoescaping is always on.
    $options['autoescape'] = 'html';
+12 −2
Original line number Diff line number Diff line
@@ -439,8 +439,15 @@ public function escapeFilter(Environment $env, $arg, $strategy = 'html', $charse

    $this->bubbleArgMetadata($arg);

    // Immediately cast and return MarkupInterface objects to a string to ensure
    // that when Twig renders via yield, later manipulations to the object will
    // not affect rendering.
    if ($autoescape && ($arg instanceof MarkupInterface)) {
      return new TwigMarkup($arg, $env->getCharset());
    }

    // Keep \Twig\Markup objects intact to support autoescaping.
    if ($autoescape && ($arg instanceof TwigMarkup || $arg instanceof MarkupInterface)) {
    if ($autoescape && ($arg instanceof TwigMarkup)) {
      return $arg;
    }

@@ -470,7 +477,10 @@ public function escapeFilter(Environment $env, $arg, $strategy = 'html', $charse
    // We have a string or an object converted to a string: Autoescape it!
    if (isset($return)) {
      if ($autoescape && $return instanceof MarkupInterface) {
        return $return;
        // Convert MarkupInterface objects to TwigMarkup objects to ensure
        // that when Twig renders via yield, later manipulations to the object
        // will not affect rendering.
        return new TwigMarkup($return, $env->getCharset());
      }
      // Drupal only supports the HTML escaping strategy, so provide a
      // fallback for other strategies.
+38 −0
Original line number Diff line number Diff line
<?php

declare(strict_types=1);

namespace Drupal\twig_fibers_test\TwigExtension;

use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;

/**
 * Custom Twig extension that suspends a fiber.
 */
class FibersTestExtension extends AbstractExtension {

  /**
   * {@inheritdoc}
   */
  public function getFunctions(): array {
    return [
      new TwigFunction('fibers_test_function', [$this, 'fibersTestFunction']),
    ];
  }

  /**
   * Custom Twig function that calls Fiber::suspend().
   *
   * @param string $message
   *   The message to return.
   *
   * @return string
   *   The processed message.
   */
  public function fibersTestFunction(string $message): string {
    \Fiber::suspend();
    return 'Fibers test: ' . $message;
  }

}
+5 −0
Original line number Diff line number Diff line
name: 'Twig Fibers Test'
type: module
description: 'Test module for Twig fiber suspend support'
package: Testing
version: VERSION
Loading