This repository has been archived by the owner on Jan 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Start of the TextExtension. Provides truncate filter.
- Loading branch information
1 parent
d712345
commit abe64b9
Showing
2 changed files
with
54 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
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 | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,53 @@ | |||
<?php | |||
|
|||
namespace TwigPlugin\Extension; | |||
|
|||
class TextExtension extends \Twig_Extension | |||
{ | |||
/** | |||
* @var \TextHelper | |||
*/ | |||
protected $textHelper; | |||
|
|||
public function __construct($view) | |||
{ | |||
\App::import('Helper', 'Text'); | |||
$this->textHelper = new \TextHelper($view); | |||
$this->request = $this->textHelper->request; | |||
$this->response = $this->textHelper->response; | |||
} | |||
|
|||
public function getFilters() | |||
{ | |||
return array( | |||
'truncate' => new \Twig_Filter_Method($this, 'truncate', | |||
array( | |||
'is_safe' => array('html'), | |||
) | |||
), | |||
); | |||
} | |||
|
|||
/** | |||
* Provides link_to function which wraps HtmlHelper::link(). | |||
* | |||
* @param $var String String to be truncated. | |||
* @param $length Integer Length to truncate. | |||
* @param $options Array Options | |||
* | |||
* Default options: 'ending' => '...', 'extact' => true, 'html' => false | |||
* | |||
* Set 'html' option to true if you want truncate | |||
* to handle HTML in the string correctly. | |||
* | |||
*/ | |||
public function truncate($var, $length = 100, array $options = array()) | |||
{ | |||
return $this->textHelper->truncate($var, $length, $options); | |||
} | |||
|
|||
public function getName() | |||
{ | |||
return 'TextHelper'; | |||
} | |||
} |