0

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.

1

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
0

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

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.