forked from twigphp/Twig-extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Text extension with truncate filter.
- Loading branch information
Showing
1 changed file
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |