X-Cart: shopping cart software

X-Cart forums (https://forum.x-cart.com/index.php)
-   Dev Questions (X-Cart 5) (https://forum.x-cart.com/forumdisplay.php?f=56)
-   -   Truncating text with an Output Filter (https://forum.x-cart.com/showthread.php?t=73100)

xgarb 11-18-2015 03:24 AM

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.

totaltec 11-19-2015 01:46 PM

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.

qualiteam 11-19-2015 11:43 PM

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.


All times are GMT -8. The time now is 11:42 AM.

Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.