Forums / Developer / Contrib : str_replace template operator

Contrib : str_replace template operator

Author Message

Lex 007

Friday 22 April 2005 3:39:56 am

Hi everyone,

Since I didn't find any str_replace template operator (maybe I didn't look right??), I just programmed a small extension for it. Very easy and very usefull (it took me more time to write this topic than programm it lol)

1/ Go to your "extension" folder in your eZ Publish install

2/ Create file str_replace/autoloads/eztemplateautoload.php

<?php

// Operator autoloading

$eZTemplateOperatorArray = array();

$eZTemplateOperatorArray[] =
  array( 'script' => 'extension/str_replace/autoloads/str_replace_controloperator.php',
         'class' => 'MyStrReplaceOperator',
         'operator_names' => array( 'ezstr_replace' ) );

?>

3/ Create file str_replace/autoloads/str_replace_controloperator.php

<?php

class MyStrReplaceOperator
{
    /*!
     Constructor
    */
    function MyStrReplaceOperator()
    {
        $this->Operators = array( 'ezstr_replace');
    }

    /*!
     Returns the operators in this class.
    */
    function &operatorList()
    {
        return $this->Operators;
    }

    /*!
     \return true to tell the template engine that the parameter list
    exists per operator type, this is needed for operator classes
    that have multiple operators.
    */
    function namedParameterPerOperator()
    {
        return true;
    }

    /*!
     The first operator has two parameters, the other has none.
     See eZTemplateOperator::namedParameterList()
    */
    function namedParameterList()
    {
        return array(                      
                      'ezstr_replace' => array('search' => array( 'type' => 'string',
                                                                     'required' => true,
                                                                     'default' => '' ),
                                                'replace' => array( 'type' => 'string',
                                                                     'required' => true,
                                                                     'default' => '' ),
                                                'subject' => array( 'type' => 'string',
                                                                     'required' => true,
                                                                     'default' => '' )
                                            ) );
    }

    /*!
     Executes the needed operator(s).
     Checks operator names, and calls the appropriate functions.
    */
    function modify( &$tpl, &$operatorName, &$operatorParameters, &$rootNamespace,
                     &$currentNamespace, &$operatorValue, &$namedParameters )
    {
        switch ( $operatorName )
        {
            case 'ezstr_replace':
            {
                $operatorValue = $this->ezstr_replace( $namedParameters['search'], 
                                                        $namedParameters['replace'], 
                                                        $namedParameters['subject']);
            } break;
        }
    }

    function ezstr_replace( $search, $replace, $subject  )
    { 
        return str_replace( $search, $replace, $subject  );
    }

    /// \privatesection
    var $Operators;
}

?>

4/ Create file str_replace/settings/site.ini.append.php

<?php

[TemplateSettings]
ExtensionAutoloadPath[]=str_replace

?>

5/ Activate the extension in the admin interface

6/ To use it in a template :

{ezstr_replace($search,$replace,$subject)}

Enjoy !

Bruce Morrison

Friday 22 April 2005 8:40:30 pm

This would be good to include in the core distribution!

My Blog: http://www.stuffandcontent.com/
Follow me on twitter: http://twitter.com/brucemorrison
Consolidated eZ Publish Feed : http://friendfeed.com/rooms/ez-publish

kracker (the)

Friday 22 April 2005 10:49:30 pm

Lex,

Would you package up your operator as an eZ publish extension (packaged in an either zip or tar.gz) and upload it into the contributions section as a new project?

And if your interested in making your operator available for consideration into eZ publish you would want to be sure to clearly assign copyright to eZ systems.

More information on this subject is available on the "Contributing Code" Section (Towards the Bottom, <i>boy, I sure wish more ez.no's content had hidden anchors for direct linking to core information and subjects</i>)
http://ez.no/community/developer/contributing

cheers,
//kracker

sole : selling live water : Respect Pt. 3

Member since: 2001.07.13 || http://ezpedia.se7enx.com/

Lex 007

Monday 25 April 2005 6:36:37 am

OK, why not !
How to you make a *.ezpkg from an extension ?

kracker (the)

Monday 25 April 2005 6:46:29 am

right on,

sorry for the confusion, i don't think you can create an extension as an eZ package. I guess the next best alternative is a documented (zip/tar.gz package).

//kracker
what was I thinking?

<i>counting crows : hey monkey, where you been?</i>

Member since: 2001.07.13 || http://ezpedia.se7enx.com/

Lex 007

Tuesday 26 April 2005 1:52:30 am

That's right ! I'll try to published it soon then.

Lex 007

Wednesday 27 April 2005 8:17:53 am

I've uploaded the files to : http://ez.no/community/contribs/template_plugins/string_replace_operator

Please report any bug.