Smarty html substr modifier

Talk about writing modules and plugins for CMS Made Simple, or about specific core functionality. This board is for PHP programmers that are contributing to CMSMS not for site developers
Locked
Guido
Forum Members
Forum Members
Posts: 221
Joined: Sat Aug 29, 2009 3:00 pm

Smarty html substr modifier

Post 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 );
    }
}
?>
User avatar
Jo Morg
Dev Team Member
Dev Team Member
Posts: 1922
Joined: Mon Jan 29, 2007 4:47 pm

Re: Smarty html substr modifier

Post 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.
"There are 10 types of people in this world, those who understand binary... and those who don't."
* by the way: English is NOT my native language (sorry for any mistakes...).
Code of Condut | CMSMS Docs | Help Support CMSMS
My developer Page on the Forge
GeekMoot 2015 in Ghent, Belgium: I was there!
GeekMoot 2016 in Leicester, UK: I was there!
DevMoot 2023 in Cynwyd, Wales: I was there!
Guido
Forum Members
Forum Members
Posts: 221
Joined: Sat Aug 29, 2009 3:00 pm

Re: Smarty html substr modifier

Post 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).
User avatar
Jo Morg
Dev Team Member
Dev Team Member
Posts: 1922
Joined: Mon Jan 29, 2007 4:47 pm

Re: Smarty html substr modifier

Post 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.
"There are 10 types of people in this world, those who understand binary... and those who don't."
* by the way: English is NOT my native language (sorry for any mistakes...).
Code of Condut | CMSMS Docs | Help Support CMSMS
My developer Page on the Forge
GeekMoot 2015 in Ghent, Belgium: I was there!
GeekMoot 2016 in Leicester, UK: I was there!
DevMoot 2023 in Cynwyd, Wales: I was there!
Guido
Forum Members
Forum Members
Posts: 221
Joined: Sat Aug 29, 2009 3:00 pm

Re: Smarty html substr modifier

Post 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?
User avatar
Jo Morg
Dev Team Member
Dev Team Member
Posts: 1922
Joined: Mon Jan 29, 2007 4:47 pm

Re: Smarty html substr modifier

Post 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.
"There are 10 types of people in this world, those who understand binary... and those who don't."
* by the way: English is NOT my native language (sorry for any mistakes...).
Code of Condut | CMSMS Docs | Help Support CMSMS
My developer Page on the Forge
GeekMoot 2015 in Ghent, Belgium: I was there!
GeekMoot 2016 in Leicester, UK: I was there!
DevMoot 2023 in Cynwyd, Wales: I was there!
Locked

Return to “Developers Discussion”