Skip to content
Browse files

Added more TextHelper methods as filters.

  • Loading branch information...
1 parent 1256577 commit 54c0e2f3c1e2c216a5ede89e6017d4890a1e5546 @paulredmond committed
Showing with 110 additions and 1 deletion.
  1. +110 −1 Lib/TwigPlugin/Extension/TextExtension.php
View
111 Lib/TwigPlugin/Extension/TextExtension.php
@@ -5,7 +5,7 @@
class TextExtension extends \Twig_Extension
{
/**
- * @var \TextHelper
+ * @var \TextHelper $textHelper
*/
protected $textHelper;
@@ -25,6 +25,31 @@ public function getFilters()
'is_safe' => array('html'),
)
),
+ 'highlight' => new \Twig_Filter_Method($this, 'highlight',
+ array(
+ 'is_safe' => array('html'),
+ )
+ ),
+ 'autoLink' => new \Twig_Filter_Method($this, 'autoLink',
+ array(
+ 'is_safe' => array('html'),
+ )
+ ),
+ 'autoLinkUrls' => new \Twig_Filter_Method($this, 'autoLinkUrls',
+ array(
+ 'is_safe' => array('html'),
+ )
+ ),
+ 'autoLinkEmails' => new \Twig_Filter_Method($this, 'autoLinkEmails',
+ array(
+ 'is_safe' => array('html'),
+ )
+ ),
+ 'stripLinks' => new \Twig_Filter_Method($this, 'stripLinks',
+ array(
+ 'is_safe' => array('html'),
+ )
+ ),
);
}
@@ -46,6 +71,90 @@ public function truncate($var, $length = 100, array $options = array())
return $this->textHelper->truncate($var, $length, $options);
}
+ /**
+ * TextHelper::highlight()
+ *
+ * Highlights $needle inside filtered $var
+ *
+ * @param $var
+ * @param $needle
+ * @param array $options
+ *
+ * Options:
+ * 'format' => '<span class="highlight">\1</span>'
+ * 'html' => true // Will ignore any HTML tags
+ *
+ * @return string
+ */
+ public function highlight($var, $needle, array $options = array())
+ {
+ return $this->textHelper->highlight($var, $needle, $options);
+ }
+
+ /**
+ * Convert all links and email addresses to HTML links.
+ *
+ * ### Options
+ *
+ * - `escape` Control HTML escaping of input. Defaults to true.
+ *
+ * @param $var Text
+ * @param array $options Array of HTML options
+ * @return string The text with links
+ * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::autoLink
+ */
+ public function autoLink($var, array $options = array())
+ {
+ return $this->textHelper->autoLink($var, $options);
+ }
+
+ /**
+ * Adds links (<a href=....) to a given text, by finding text that begins with
+ * strings like http:// and ftp://.
+ *
+ * ### Options
+ *
+ * - `escape` Control HTML escaping of input. Defaults to true.
+ *
+ * @param $var Text
+ * @param array $options Array of options
+ * @return string The text with links.
+ * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::autoLinkUrls
+ */
+ public function autoLinkUrls($var, array $options = array())
+ {
+ return $this->textHelper->autoLinkUrls($var, $options);
+ }
+
+ /**
+ * Adds email links (<a href="mailto:....) to a given text.
+ *
+ * ### Options
+ *
+ * - `escape` Control HTML escaping of input. Defaults to true.
+ *
+ * @param $var Text
+ * @param array $options Array of options
+ * @return string Text with email links.
+ * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::autoLinkEmails
+ */
+ public function autoLinkEmails($var, array $options = array())
+ {
+ return $this->textHelper->autoLinkEmails($var, $options);
+ }
+
+ /**
+ * @see String::stripLinks()
+ *
+ * @param $var Text
+ * @return string The text without links.
+ * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/text.html#TextHelper::stripLinks
+ */
+ public function stripLinks($var)
+ {
+ return $this->textHelper->stripLinks($var);
+ }
+
public function getName()
{
return 'TextHelper';

0 comments on commit 54c0e2f

Please sign in to comment.
Something went wrong with that request. Please try again.