You are here

public function TwigExtension::truncate in Twig Tweak 8.2

Truncates a UTF-8-encoded string safely to a number of characters.

Parameters

string $string: The string to truncate.

int $max_length: An upper limit on the returned string length, including trailing ellipsis if $add_ellipsis is TRUE.

bool $wordsafe: (optional) If TRUE, attempt to truncate on a word boundary.

bool $add_ellipsis: (optional) If TRUE, add '...' to the end of the truncated string.

int $min_wordsafe_length: (optional) If TRUE, the minimum acceptable length for truncation.

Return value

string The truncated string.

See also

\Drupal\Component\Utility\Unicode::truncate()

File

src/TwigExtension.php, line 1147

Class

TwigExtension
Twig extension with some useful functions and filters.

Namespace

Drupal\twig_tweak

Code

public function truncate($string, $max_length, $wordsafe = FALSE, $add_ellipsis = FALSE, $min_wordsafe_length = 1) {
  return Unicode::truncate($string, $max_length, $wordsafe, $add_ellipsis, $min_wordsafe_length);
}