Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add a truncate filter to the Twig helpers

  • Loading branch information...
commit 95b5dea9db39a4de1df38b8fb6a76777bd768323 1 parent d2e3331
@kolber authored
Showing with 14 additions and 1 deletion.
  1. +14 −1 extensions/twig-extensions.inc.php
View
15 extensions/twig-extensions.inc.php
@@ -14,7 +14,8 @@ public function getFilters() {
# custom twig filters
return array(
'absolute' => new Twig_Filter_Method($this, 'absolute'),
- 'context' => new Twig_Filter_Method($this, 'context')
+ 'context' => new Twig_Filter_Method($this, 'context'),
+ 'truncate' => new Twig_Filter_Method($this, 'truncate')
);
}
@@ -169,6 +170,18 @@ function absolute($relative_path) {
return $server_name.str_replace('/index.php', $relative_path, $_SERVER['SCRIPT_NAME']);
}
+ function truncate($value, $length = 30, $preserve = false, $separator = '...') {
+ if (strlen($value) > $length) {
+ if ($preserve) {
+ if (false !== ($breakpoint = strpos($value, ' ', $length))) {
+ $length = $breakpoint;
+ }
+ }
+ return substr($value, 0, $length) . $separator;
+ }
+ return $value;
+ }
+
}
?>
Please sign in to comment.
Something went wrong with that request. Please try again.