| 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 Exponential 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 ! |