Ticket #545: template_virtuel.diff

File template_virtuel.diff, 5.7 kB (added by Lipki, 9 months ago)
  • lib/jelix/core/jIncluder.class.php

    old new  
    9898        $mustCompile = $gJConfig->compilation['force'] || !file_exists($cachefile); 
    9999        $sourcefile = $aSelector->getPath(); 
    100100 
    101         if($sourcefile == '' || !file_exists($sourcefile)){ 
     101        if( ( $sourcefile == '' || !file_exists($sourcefile) ) && !$aSelector->_dyn ){ 
    102102            throw new jException('jelix~errors.includer.source.missing',array( $aSelector->toString(true))); 
    103103        } 
    104104 
  • lib/jelix/core/jSelector.class.php

    old new  
    104104    protected $_cachePath; 
    105105    protected $_compiler = null; 
    106106    protected $_compilerPath; 
     107    protected $_dyn = false; 
    107108    protected $_useMultiSourceCompiler=false; 
    108109 
    109110    function __construct($sel){ 
     
    709710    } 
    710711} 
    711712 
     713 
     714 
     715class jSelectorVTpl extends jSelectorModule { 
     716    protected $type = 'tpl'; 
     717    protected $_dirname = 'classes/'; 
     718    protected $_suffix = '.tpl'; 
     719    protected $_where; 
     720    public $outputType=''; 
     721    public $trusted=true; 
     722    public $_dyn = true; 
     723        function __construct($sel, $outputtype='', $trusted=true) { 
     724        if($outputtype == ''){ 
     725            if($GLOBALS['gJCoord']->response) 
     726                $this->outputType = $GLOBALS['gJCoord']->response->getFormatType(); 
     727            else 
     728                $this->outputType = $GLOBALS['gJCoord']->request->defaultResponseType; 
     729        } else 
     730            $this->outputType = $outputtype; 
     731        $this->trusted = $trusted; 
     732        $this->_compiler='jVTplCompiler'; 
     733        $this->_compilerPath=JELIX_LIB_PATH.'tpl/jTplCompiler.class.php'; 
     734        parent::__construct($sel); 
     735    } 
     736    protected function _createPath(){ 
     737        global $gJConfig; 
     738        if(!isset($gJConfig->_modulesPathList[$this->module])){ 
     739            throw new jExceptionSelector('jelix~errors.selector.module.unknow', $this->toString()); 
     740        } 
     741                $path = $this->module.'/'.$this->resource; 
     742                // pas d'overload via themes/ 
     743                // pas de local 
     744        $this->_path = $gJConfig->_modulesPathList[$this->module].$this->_dirname.'dynTpl.class.php'; 
     745        if(is_readable($this->_path)){ 
     746            $this->_where = 'modules/'.$path; 
     747            return; 
     748        } 
     749                throw new jExceptionSelector('jelix~errors.selector.invalid.target', array($this->toString(), "dyn template")); 
     750    } 
     751    protected function _createCachePath(){ 
     752       $this->_cachePath = JELIX_APP_TEMP_PATH.'compiled/templates/'.$this->_where.'_'.$this->outputType.($this->trusted?'_t':'').$this->_cacheSuffix; 
     753    } 
     754} 
     755 
     756 
     757 
    712758/** 
    713759 * Zone selector 
    714760 * 
  • lib/jelix/tpl/jTpl.class.php

    old new  
    242242     * @param boolean $callMeta false if meta should not be called 
    243243     * @return string the generated content 
    244244     */ 
    245     public function fetch ($tpl, $outputtype='', $trusted = true, $callMeta=true){ 
     245    public function fetch ($tpl, $outputtype='', $trusted = true, $callMeta=true, $dyn=false){ 
    246246        $content = ''; 
    247247        ob_start (); 
    248248        try{ 
    249249#ifnot JTPL_STANDALONE 
    250             $sel = new jSelectorTpl($tpl, $outputtype, $trusted); 
     250            if( $dyn ) 
     251                sel = new jSelectorVTpl($tpl, $outputtype, $trusted); 
     252            else $sel = new jSelectorTpl($tpl, $outputtype, $trusted); 
    251253            jIncluder::inc($sel); 
    252254            $md = md5($sel->module.'_'.$sel->resource.'_'.$sel->outputType.($trusted?'_t':'')); 
    253255#else 
     
    283285        } 
    284286        return $content; 
    285287    } 
     288     
     289     public function dynFetch($tpl, $outputtype='', $trusted = true, $callMeta=true){ 
     290        return $this->fetch($tpl, $outputtype, $trusted, $callMeta, true); 
     291     } 
    286292 
    287293    /** 
    288294     * deprecated function: optimized version of meta() + fetch(). 
  • lib/jelix/tpl/jTplCompiler.class.php

    old new  
    592592 
    593593} 
    594594 
     595class jVTplCompiler extends jTplCompiler { 
     596    private $_pluginPath=array(); 
     597    private $_metaBody = ''; 
     598         
     599    public function compile($selector){ 
     600         
     601        $this->dynFile = $selector->getPath(); 
     602        $cachefile = $selector->getCompiledFilePath(); 
     603        $this->outputType = $selector->outputType; 
     604        $this->trusted = $selector->trusted; 
     605        jContext::push($selector->module); 
     606                 
     607        require_once($this->dynFile); 
     608        $dyn = new dynTpl(); 
     609        if( !is_callable(array($dyn, $selector->resource)) ) { 
     610              $this->doError0('errors.tpl.not.found'); 
     611        } 
     612                 
     613        $result = $this->compileContent($dyn->{$selector->resource}()); 
     614                 
     615        $header ="<?php \n"; 
     616        foreach($this->_pluginPath as $path=>$ok){ 
     617            $header.=' require_once(\''.$path."');\n"; 
     618        } 
     619        $header.='function template_meta_'.md5($selector->module.'_'.$selector->resource.'_'.$this->outputType.($this->trusted?'_t':'')).'($t){'; 
     620        $header .="\n".$this->_metaBody."\n}\n"; 
     621        $header.='function template_'.md5($selector->module.'_'.$selector->resource.'_'.$this->outputType.($this->trusted?'_t':'')).'($t){'."\n?>"; 
     622        $result = $header.$result."<?php \n}\n?>"; 
     623        jFile::write($cachefile, $result); 
     624        jContext::pop(); 
     625        return true; 
     626    } 
     627} 
     628 
    595629?> 
Download in other formats: Original Format