Permalink
Browse files

Start of the TextExtension. Provides truncate filter.

  • Loading branch information...
1 parent d712345 commit abe64b974b51afa5ecac8273d770d9e5a97337c0 @paulredmond committed Aug 1, 2012
Showing with 54 additions and 0 deletions.
  1. +1 −0 Config/bootstrap.php
  2. +53 −0 Lib/TwigPlugin/Extension/TextExtension.php
View
@@ -22,6 +22,7 @@
'TwigPlugin\Extension\BasicExtension',
'TwigPlugin\Extension\HtmlExtension',
'TwigPlugin\Extension\FormExtension',
+ 'TwigPlugin\Extension\TextExtension',
),
(array) Configure::read('twig.extensions')
));
@@ -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.