Skip to content

Commit

Permalink
Add a truncate filter to the Twig helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
kolber committed Apr 5, 2013
1 parent d2e3331 commit 95b5dea
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion extensions/twig-extensions.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')
);
}

Expand Down Expand Up @@ -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;
}

}

?>

0 comments on commit 95b5dea

Please sign in to comment.