Changeset 998

Show
Ignore:
Timestamp:
06/21/08 17:08:23 (2 months ago)
Author:
laurentj
Message:

ticket #138: added registerModifier and registerFunction into jTpl

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/lib/jelix/core/jSelector.class.php

    r939 r998  
    644644    public $outputType=''; 
    645645    public $trusted=true; 
     646    public $userModifiers = array(); 
     647    public $userFunctions = array(); 
     648 
    646649 
    647650    /** 
  • trunk/lib/jelix/tpl/jTpl.class.php

    r873 r998  
    55* @author      Laurent Jouanneau 
    66* @contributor Dominique Papin 
    7 * @copyright   2005-2006 Laurent Jouanneau, 2007 Dominique Papin 
     7* @copyright   2005-2008 Laurent Jouanneau, 2007 Dominique Papin 
    88* @link        http://www.jelix.org 
    99* @licence     GNU Lesser General Public Licence see LICENCE file or http://www.gnu.org/licenses/lgpl.html 
     
    207207#ifnot JTPL_STANDALONE 
    208208        $sel = new jSelectorTpl($tpl,$outputtype,$trusted); 
     209        $sel->userModifiers = $this->userModifiers; 
     210        $sel->userFunctions = $this->userFunctions; 
    209211        jIncluder::inc($sel); 
    210212        $fct = $fctname.md5($sel->module.'_'.$sel->resource.'_'.$sel->outputType.($trusted?'_t':'')); 
     
    225227 
    226228            $compiler = new jTplCompiler(); 
     229            $compiler->setUserPlugins( $this->userModifiers, $this->userFunctions); 
    227230            $compiler->compile($tpl,$outputtype, $trusted); 
    228231        } 
     
    297300 
    298301 
     302    protected $userModifiers = array(); 
     303 
     304    /** 
     305     * register a user modifier. The function should accept at least a 
     306     * string as first parameter, and should return this string 
     307     * which can be modified. 
     308     * @param string $name  the name of the modifier in a template 
     309     * @param string $functionName the corresponding PHP function 
     310     * @since jelix 1.1 
     311     */ 
     312    public function registerModifier($name, $functionName) { 
     313        $this->userModifiers[$name] = $functionName; 
     314    } 
     315 
     316    protected $userFunctions = array(); 
     317 
     318    /** 
     319     * register a user function. The function should accept a jTpl object 
     320     * as first parameter. 
     321     * @param string $name  the name of the modifier in a template 
     322     * @param string $functionName the corresponding PHP function 
     323     * @since jelix 1.1 
     324     */ 
     325    public function registerFunction($name, $functionName) { 
     326        $this->userFunctions[$name] = $functionName; 
     327    } 
     328 
    299329    /** 
    300330     * return the current encoding 
  • trunk/lib/jelix/tpl/jTplCompiler.class.php

    r997 r998  
    5757    private $_metaBody = ''; 
    5858 
    59     private $_modifier = array('upper'=>'strtoupper', 'lower'=>'strtolower', 
     59    protected $_modifier = array('upper'=>'strtoupper', 'lower'=>'strtolower', 
    6060        'escxml'=>'htmlspecialchars', 'eschtml'=>'htmlspecialchars', 'strip_tags'=>'strip_tags', 'escurl'=>'rawurlencode', 
    6161        'capitalize'=>'ucwords', 'stripslashes'=>'stripslashes' 
     
    6868    public $outputType=''; 
    6969    public $trusted=true; 
     70 
     71    protected $_userFunctions = array(); 
    7072 
    7173    /** 
     
    9395     * @return boolean true if ok 
    9496     */ 
    95     public function compile($tplFile, $outputtype, $trusted){ 
     97    public function compile($tplFile, $outputtype, $trusted, $userModifiers = array(), $userFunctions = array()){ 
    9698        $this->_sourceFile = $tplFile; 
    9799        $this->outputType = ($outputtype==''?'html':$outputtype); 
    98100        $cachefile = JTPL_CACHE_PATH .$this->outputType.($trusted?'_t':'').'_'. basename($tplFile); 
    99101        $this->trusted = $trusted; 
     102        $this->_modifier = array_merge($this->_modifier, $userModifiers); 
     103        $this->_userFunctions = $userFunctions; 
     104 
    100105#else 
    101106    /** 
     
    111116        $this->outputType = $selector->outputType; 
    112117        $this->trusted = $selector->trusted; 
     118        $this->_modifier = array_merge($this->_modifier, $selector->userModifiers); 
     119        $this->_userFunctions = $selector->userFunctions; 
     120 
    113121        jContext::push($selector->module); 
    114122#endif 
     
    393401                    $res = $path[1].'( $t'.(trim($argfct)!=''?','.$argfct:'').');'; 
    394402                    $this->_pluginPath[$path[0]] = true; 
     403 
     404                } else if ( isset($this->_userFunctions[$name])) { 
     405                    $argfct=$this->_parseFinal($args,$this->_allowedAssign); 
     406                    $res = $this->_userFunctions[$name].'( $t'.(trim($argfct)!=''?','.$argfct:'').');'; 
    395407 
    396408                } else { 
  • trunk/testapp/modules/jelix_tests/tests/jtpl.compiler.html_cli.php

    r958 r998  
    1414class testJtplContentCompiler extends jTplCompiler { 
    1515 
    16    public function compileContent2($content){ 
     16    public function setUserPlugins($userModifiers, $userFunctions) { 
     17        $this->_modifier = array_merge($this->_modifier, $userModifiers); 
     18        $this->_userFunctions = $userFunctions; 
     19    } 
     20 
     21    public function compileContent2($content){ 
    1722        return $this->compileContent($content); 
    18    } 
    19 
    20  
    21  
     23    } 
     24
     25 
     26function testjtplcontentUserFunction($t,$a,$b) { 
     27 
     28
    2229 
    2330 
     
    119126        '<p>ok<?php if(($t->_vars[\'foo\'] || $t->_vars[\'bar\']) && $t->_vars[\'baz\']):?> <?php endif;?></p>', 
    120127        ), 
     12819=>array( 
     129        '<p>ok{bla $foo, $params}</p>', 
     130        '<p>ok<?php testjtplcontentUserFunction( $t,$t->_vars[\'foo\'], $t->_vars[\'params\']);?></p>', 
     131        ), 
    121132    ); 
    122133 
     
    125136        $compil->outputType = 'html'; 
    126137        $compil->trusted = true; 
    127  
     138        $compil->setUserPlugins(array(), array('bla'=>'testjtplcontentUserFunction')); 
    128139        foreach($this->content as $k=>$t){ 
    129140            try{ 
  • trunk/testapp/modules/jelix_tests/tests/jtpl.expressions_parsing.html_cli.php

    r997 r998  
    1414class testJtplCompiler extends jTplCompiler { 
    1515 
     16    public function setUserPlugins($userModifiers, $userFunctions) { 
     17        $this->_modifier = array_merge($this->_modifier, $userModifiers); 
     18        $this->_userFunctions = $userFunctions; 
     19    } 
     20 
     21 
    1622   public function testParseExpr($string, $allowed=array(), $exceptchar=array(';'), $splitArgIntoArray=false){ 
    1723        return $this->_parseFinal($string, $allowed, $exceptchar, $splitArgIntoArray); 
     
    4450} 
    4551 
    46  
     52function testjtplcontentUserModifier($s){} 
    4753 
    4854 
     
    262268        '$aaa|jdatetime:\'db_date\',\'lang_date\'' => 'jtpl_modifier_common_jdatetime($t->_vars[\'aaa\'],\'db_date\',\'lang_date\')', 
    263269        '$aaa|jdatetime:\'db_:date\',\'lang_date\'' => 'jtpl_modifier_common_jdatetime($t->_vars[\'aaa\'],\'db_:date\',\'lang_date\')', 
     270        '$aaa|bla'=>'testjtplcontentUserModifier($t->_vars[\'aaa\'])', 
    264271    ); 
    265272 
     
    268275        $compil = new testJtplCompiler(); 
    269276        $compil->trusted = true; 
     277        $compil->setUserPlugins(array('bla'=>'testjtplcontentUserModifier'),array()); 
     278 
    270279        foreach($this->varTag as $k=>$t){ 
    271280            try{ 
Download in other formats: Unified Diff Zip Archive