Skip to content

Commit

Permalink
Added Text extension with truncate filter.
Browse files Browse the repository at this point in the history
  • Loading branch information
henrikbjorn authored and fabpot committed Feb 19, 2010
1 parent 4b51da4 commit 33fde50
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions Extension/Text.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

/**
* This file is part of Twig.
*
* (c) 2009 Fabien Potencier
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @author Henrik Bjornskov <hb@peytz.dk>
* @package Twig
* @subpackage Twig-extensions
*/
class Twig_Extension_Text extends Twig_Extension
{
/**
* Returns a list of filters.
*
* @return array
*/
public function getFilters()
{
return array(
'truncate' => new Twig_Filter_Function('twig_truncate_filter', array('needs_environment' => true)),
);
}

/**
* Name of this extension
*
* @return string
*/
public function getName()
{
return 'Text';
}
}

if (function_exists('mb_get_info'))
{
function twig_truncate_filter(Twig_Environment $env, $value, $length = 30, $separator = '...')
{
if (mb_strlen($value, $env->getCharset()) > $length)
{
return mb_substr($value, 0, $length, $env->getCharset()).$separator;
}

return $value;
}
}
else
{
function twig_truncate_filter(Twig_Environment $env, $value, $length = 30, $separator = '...')
{
if (strlen($value) > $length)
{
return substr($value, 0, $length).$separator;
}

return $value;
}
}

0 comments on commit 33fde50

Please sign in to comment.