Page 1 of 1

Smarty html substr modifier

Posted: Fri May 29, 2015 8:53 am
by Guido
I used to use truncate_better for cmsms, but since the latest versions, this doesn't work anymore. I found this Smarty modifier online at http://stackoverflow.com/questions/1417 ... y-variable and it works with the latest CMSMS version. Would it be an idea to add this to the CMSMS core?

BTW, here are the including instructions:
Simply add a new php file called "modifier.html_substr.php" into the smarty/libs/plugins directory
Usage:

{$products_data.PRODUCTS_DESCRIPTION|html_substr:300:' ...'}

Code: Select all

<?php
/*
* Smarty plugin
*
-------------------------------------------------------------
* File: modifier.html_substr.php
* Type: modifier
* Name: html_substr
* Version: 1.0
* Date: June 19th, 2003
* Purpose: Cut a string preserving any tag nesting and matching.
* Install: Drop into the plugin directory.
* Author: Original Javascript Code: Benjamin Lupu <hide@address.com>
* Translation to PHP & Smarty: Edward Dale <hide@address.com>
* Modification to add a string: Sebastian Kuhlmann <hide@address.com>
* Modification to put the added string before closing <p> or <li> tags by Peter Carter http://www.podhawk.com
-------------------------------------------------------------
*/
function smarty_modifier_html_substr($string, $length, $addstring="")
{

//some nice italics for the add-string
 if (!empty($addstring)) $addstring = "<i> " . $addstring . "</i>";

if (strlen($string) > $length) {
    if( !empty( $string ) && $length>0 ) {
        $isText = true;
        $ret = "";
        $i = 0;

        $currentChar = "";
        $lastSpacePosition = -1;
        $lastChar = "";

        $tagsArray = array();
        $currentTag = "";
        $tagLevel = 0;

        $addstringAdded = false;

        $noTagLength = strlen( strip_tags( $string ) );

        // Parser loop
        for( $j=0; $j<strlen( $string ); $j++ ) {

            $currentChar = substr( $string, $j, 1 );
            $ret .= $currentChar;

            // Lesser than event
            if( $currentChar == "<") $isText = false;

                // Character handler
                if( $isText ) {

                    // Memorize last space position
                    if( $currentChar == " " ) { $lastSpacePosition = $j; }
                    else { $lastChar = $currentChar; }

                    $i++;
                    } else {
                    $currentTag .= $currentChar;
                    }

                    // Greater than event
                    if( $currentChar == ">" ) {
                        $isText = true;

                        // Opening tag handler
                        if( ( strpos( $currentTag, "<" ) !== FALSE ) &&
                        ( strpos( $currentTag, "/>" ) === FALSE ) &&
                        ( strpos( $currentTag, "</") === FALSE ) ) {

                        // Tag has attribute(s)
                        if( strpos( $currentTag, " " ) !== FALSE ) {
                            $currentTag = substr( $currentTag, 1, strpos( $currentTag, " " ) - 1 );
                            } else {
                            // Tag doesn't have attribute(s)
                            $currentTag = substr( $currentTag, 1, -1 );
                            }

                            array_push( $tagsArray, $currentTag );

                            } else if( strpos( $currentTag, "</" ) !== FALSE ) {
                            array_pop( $tagsArray );
                            }

                        $currentTag = "";
                        }

                    if( $i >= $length) {
                    break;
                    }
                }

        // Cut HTML string at last space position
        if( $length < $noTagLength ) {
            if( $lastSpacePosition != -1 ) {
            $ret = substr( $string, 0, $lastSpacePosition );
            } else {
            $ret = substr( $string, $j );
            }
        }

        // Close broken XHTML elements
            while( sizeof( $tagsArray ) != 0 ) {
            $aTag = array_pop( $tagsArray );
                // if a <p> or <li> tag needs to be closed, put the add-string in first
                if (($aTag == "p" || $aTag == "li") && strlen($string) > $length) {
                $ret .= $addstring;
                $addstringAdded = true;
                }
            $ret .= "</" . $aTag . ">\n";
            }

        } else {
        $ret = "";
        }

    // if we have not added the add-string already 
    if ( strlen($string) > $length && $addstringAdded == false) {
        return( $ret.$addstring );
        }
        else {
        return ( $ret );
        }
    }
    else {
    return ( $string );
    }
}
?>

Re: Smarty html substr modifier

Posted: Fri May 29, 2015 9:05 am
by Jo Morg
Guido wrote:I used to use truncate_better for cmsms, but since the latest versions, this doesn't work anymore.
What do you mean exactly by this? Where did you drop this file?
The reason I'm asking is that, at some point, the Smarty folder structure changed (nothing to do with CMSMS) and the plugins folder moved from where it was on previous Smarty versions. As a consequence, on upgrading a site, custom Smarty plugins stopped working unless moved to the new plugins folder, or...
Did you know you can use CMSMS own plugins folder to drop custom Smarty plugins? :) It will work and it there are no plans to alter that functionality on CMSMS any time soon if at all.

Re: Smarty html substr modifier

Posted: Fri May 29, 2015 9:29 am
by Guido
Sorry I wasn't exact in my choice of words; it wasn't a plugin, but a UDT. I see there is a plugin in the forge for this (truncate_better) also: maybe this does work.

I dropped the file in the lib/smarty/libs/plugins folder, this works but I didn't know the CMSMS native plugins folder also registers Smarty plugins. I thought it only registered dedicated CMSMS plugins (come to think of it I'm also unclear about the exact difference between the two).

Re: Smarty html substr modifier

Posted: Fri May 29, 2015 9:39 am
by Jo Morg
Guido wrote:Sorry I wasn't exact in my choice of words; it wasn't a plugin, but a UDT.
The code you pasted is of a smarty plugin... :) so if you had save it as modifier.html_substr.php it would work in either Smarty plugins folder or CMSMS own plugin folder.
Guido wrote: I didn't know the CMSMS native plugins folder also registers Smarty plugins. I thought it only registered dedicated CMSMS plugins (come to think of it I'm also unclear about the exact difference between the two).
Well the difference is that a CMSMS plugin IS a smarty plugin but with some added functionality specifically used by CMSMS, the ability to have a help page being one of them.

Re: Smarty html substr modifier

Posted: Fri May 29, 2015 10:14 am
by Guido
The code you pasted is of a smarty plugin... :) so if you had save it as modifier.html_substr.php it would work in either Smarty plugins folder or CMSMS own plugin folder.
I meant to say I used 'truncate_better' as a UDT, and just saw there was a plugin file available as well.

The Smarty Modifier plugin I didn't use as a UDT, only as a plugin as the stackoverflow post suggested.

Ah, well I'm in the process of learning PHP so this is interesting. Basically if I understand correctly the plugins are smarty plugins, with added functionality for CMSMS?

Re: Smarty html substr modifier

Posted: Fri May 29, 2015 11:29 am
by Jo Morg
Guido wrote:Basically if I understand correctly the plugins are smarty plugins, with added functionality for CMSMS?
Correct! Check the source of some of the core and 3rd party plugins for CMSMS to see how they work.