Follow us on Twitter X-Cart on Facebook Wiki
Shopping cart software Solutions for online shops and malls
 

Truncating text with an Output Filter

 
Reply
   X-Cart forums > X-Cart 5 > Dev Questions (X-Cart 5)
 
Thread Tools Search this Thread
  #1  
Old 11-18-2015, 03:24 AM
 
xgarb xgarb is offline
 

eXpert
  
Join Date: Jul 2004
Location: UK
Posts: 263
 

Default Truncating text with an Output Filter

There's a guide here for making your own output filter.. http://kb.x-cart.com/display/XDD/Flexy+Guide

I found this method easier...

create a file called ShortenText.php in your View folder of your theme classes like this...

Code:
<?php namespace XLite\Module\Customer\CustomerTheme\View; /** * Abstract widget */ abstract class ShortenText extends \XLite\View\AView implements \XLite\Base\IDecorator { protected function flexyModifierShortenText($string) { $string = substr($string, 0, strrpos(substr($string, 0, 200), ' ')); return $string."..."; } }

Then create a copy of the template you want to add the filter to and put it in your template theme folder ie skins/Customer/CustomerTheme/en/modules/XC/News/top_news_messages/list/parts/

I'm adding the filter to the news summary to show the same number of characters. My template looks like this...

Code:
{** * @ListChild (list="itemsList.newsMessages.customer.top.row", weight="250") *} <span class='brief'>{model.brief_description:ShortenText}</span> <a href="{buildURL(#news_message#,##,_ARRAY_(#id#^model.id))}">Read more</a>

to give something like this on the page...

<snip>
they buy and... Read more


It all works well but... how do I make so I can choose the number of characters to show from the template? IE something like {model.brief_description:ShortenText(100)} to show 100 characters.
__________________
Core version: 5.5.xx
Reply With Quote

The following user thanks xgarb for this useful post:
totaltec (11-19-2015)
  #2  
Old 11-19-2015, 01:46 PM
  totaltec's Avatar 
totaltec totaltec is offline
 

X-Guru
  
Join Date: Jan 2007
Location: Louisville, KY USA
Posts: 5,823
 

Default Re: Truncating text with an Output Filter

First of all it is extremely cool that you created your own modifier!

I don't know of any examples where a modifier is passed a value. I'm sure it could be done, but you might have to modify:
Code:
/** * Call for Flexy modifier from AView class * * @param string $callMethod Name of method to call * @param string $expr Exression to modify * * @return string */ protected function flexyModifierCall($callMethod, $expr) { $callMethod = 'flexyModifier' . ucfirst($callMethod); return method_exists($this, $callMethod) ? $this->$callMethod($expr) : ''; }

Reading through the method above, I think it would have to have another argument added, like:
protected function flexyModifierCall($callMethod, $expr, $params = NULL)

Then you could pass it some parameters and use those in your call. But I am just not sure that it is wired that way. Probably I would just create a method that shortened the text with 2 arguments, value and length.
__________________
Mike White - Now Accepting new clients and projects! Work with the best, get a US based development team for just $125 an hour. Call 1-502-773-6454, email mike at babymonkeystudios.com, or skype b8bym0nkey

XcartGuru
X-cart Tutorials | X-cart 5 Tutorials

Check out the responsive template for X-cart.
Reply With Quote

The following user thanks totaltec for this useful post:
xgarb (11-20-2015)
  #3  
Old 11-19-2015, 11:43 PM
  qualiteam's Avatar 
qualiteam qualiteam is offline
 

X-Guru
  
Join Date: Dec 2010
Posts: 6,373
 

Default Re: Truncating text with an Output Filter

Quote:
Originally Posted by xgarb
how do I make so I can choose the number of characters to show from the template? IE something like {model.brief_description:ShortenText(100)} to show 100 characters.

I would just create a plain method with two arguments and used it in the template:
PHP Code:
protected function shortenText($string$length)
    {
        return 
substr($string0strrpos(substr($string0$length), ' ')) . '...';
    } 

Code:
{** * @ListChild (list="itemsList.newsMessages.customer.top.row", weight="250") *} <span class='brief'>{shortenText(model.brief_description,200)}</span> <a href="{buildURL(#news_message#,##,_ARRAY_(#id#^model.id))}">Read more</a>

However, I believe you don't need this method in _every_ widget on the page. So, it makes sense to put it into the widget class where you need it instead of decorating the base \XLite\View\AView class.
__________________
Alex Solovev,
Qualiteam

---

User manual Video tutorials X-Cart FAQ

You are welcome to press "Thanks" button
if you find this post useful

Click here to learn how to apply patches

X-Cart Extensions
Reply With Quote

The following 2 users thank qualiteam for this useful post:
razortw (11-20-2015), xgarb (11-20-2015)
Reply
   X-Cart forums > X-Cart 5 > Dev Questions (X-Cart 5)



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -8. The time now is 09:18 AM.

   

 
X-Cart forums © 2001-2020