function TwigExtension::safeJoin

Same name in other branches
  1. 9 core/lib/Drupal/Core/Template/TwigExtension.php \Drupal\Core\Template\TwigExtension::safeJoin()
  2. 8.9.x core/lib/Drupal/Core/Template/TwigExtension.php \Drupal\Core\Template\TwigExtension::safeJoin()
  3. 11.x core/lib/Drupal/Core/Template/TwigExtension.php \Drupal\Core\Template\TwigExtension::safeJoin()

Joins several strings together safely.

Parameters

\Twig\Environment $env: A Twig Environment instance.

mixed[]|\Traversable|null $value: The pieces to join.

string $glue: The delimiter with which to join the string. Defaults to an empty string. This value is expected to be safe for output and user provided data should never be used as a glue.

Return value

string The strings joined together.

File

core/lib/Drupal/Core/Template/TwigExtension.php, line 596

Class

TwigExtension
A class providing Drupal Twig extensions.

Namespace

Drupal\Core\Template

Code

public function safeJoin(Environment $env, $value, $glue = '') {
    if ($value instanceof \Traversable) {
        $value = iterator_to_array($value, FALSE);
    }
    return implode($glue, array_map(function ($item) use ($env) {
        // If $item is not marked safe then it will be escaped.
        return $this->escapeFilter($env, $item, 'html', NULL, TRUE);
    }, (array) $value));
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.