Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

Commit

Permalink
Start of the TextExtension. Provides truncate filter.
Browse files Browse the repository at this point in the history
  • Loading branch information
paulredmond committed Aug 1, 2012
1 parent d712345 commit abe64b9
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions Config/bootstrap.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
'TwigPlugin\Extension\BasicExtension', 'TwigPlugin\Extension\BasicExtension',
'TwigPlugin\Extension\HtmlExtension', 'TwigPlugin\Extension\HtmlExtension',
'TwigPlugin\Extension\FormExtension', 'TwigPlugin\Extension\FormExtension',
'TwigPlugin\Extension\TextExtension',
), ),
(array) Configure::read('twig.extensions') (array) Configure::read('twig.extensions')
)); ));
53 changes: 53 additions & 0 deletions Lib/TwigPlugin/Extension/TextExtension.php
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';
}
}

0 comments on commit abe64b9

Please sign in to comment.