I'm writing a new theme for magento 2.

I need to truncate product short description in my template. In version 1 there was a method in core/string helper, so i was able to do something like Mage::helper('core/string')->truncate($string, $length).

In Magento 2 i can't find a way to do that without rewriting a ListProduct class.


You can use \Magento\Framework\Filter\FilterManager for that:

$this->filterManager->truncate($string, ['length' => $length]);
  • i get Cannot access protected property Magento\Catalog\Block\Product\ListProduct\Interceptor::$filterManager using your way – Styopchik Sep 27 '16 at 10:21
  • @Styopchik well you will have to use dependency injection to inject it in your class constructor. – Raphael at Digital Pianism Sep 27 '16 at 10:21
  • in theme? how to do that? – Styopchik Sep 27 '16 at 10:23

for this you can use core php for this.

$string = (strlen($string) > $length) ? substr($string,0,$length).'...' : $string;
  • this way can breake html tags from short description – Styopchik Sep 27 '16 at 10:23
  • it will count string length and string length is > than youe length then it will break length – Murtuza Zabuawala Sep 27 '16 at 10:35

I'm late, i know. But i found the following solution(class) in Magento 2 Framework.


The class have same code like:

app/code/core/Mage/Core/Helper/String.php -> helper('core/string')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.