CPradoViewRenderer

CDbHttpSession

Yii Framework v1.1.10 类参考

CPradoViewRenderer

system.web.renderers
继承 class CPradoViewRenderer » CViewRenderer » CApplicationComponent » CComponent
实现 IViewRenderer, IApplicationComponent
源自 1.0
版本 $Id: CPradoViewRenderer.php 2799 2011-01-01 19:31:13Z qiang.xue $
源码 framework/web/renderers/CPradoViewRenderer.php
CPradoViewRenderer实现一个视图渲染器,它允许用户使用类似于PRADO模板的模板语法。

要使用CPradoViewRenderer, 在应该程序配置文件中配置一个名为"viewRenderer"的应用组件:
array(
    'components'=>array(
        ......
        'viewRenderer'=>array(
            'class'=>'CPradoViewRenderer',
        ),
    ),
)


CPradoViewRenderer 允许你用下面的语法来写视图文件:
// PHP tags:
<%= expression %>
// <?php echo expression ?>
<% statement %>
// <?php statement ?></li>

// component tags:
<com:WigetClass name1="value1" name2='value2' name3={value3} >
// <?php $this->beginWidget('WigetClass',
// array('name1'=>"value1", 'name2'=>'value2', 'name3'=>value3)); ?>
</com:WigetClass >
// <?php $this->endWidget('WigetClass'); ?>
<com:WigetClass name1="value1" name2='value2' name3={value3} />
// <?php $this->widget('WigetClass',
// array('name1'=>"value1", 'name2'=>'value2', 'name3'=>value3)); ?>

// cache tags:
<cache:fragmentID name1="value1" name2='value2' name3={value3} >
// <?php if($this->beginCache('fragmentID',
// array('name1'=>"value1", 'name2'=>'value2', 'name3'=>value3))): ?>
</cache:fragmentID >
// <?php $this->endCache('fragmentID'); endif; ?>

// clip tags:
<clip:clipID >
// <?php $this->beginClip('clipID'); ?>
</clip:clipID >
// <?php $this->endClip('clipID'); ?>

// comment tags:
<!--- comments --->
// the whole tag will be stripped off

公共属性

隐藏继承属性

属性类型描述定义在
behaviors array 这个应用组件附加的行为。 这此行为将在应用组件调用init时附加在应用组件上。 请参照CModel::behaviors如何指定此属性值。 CApplicationComponent
fileExtension string 视图文件的扩展名. CViewRenderer
filePermission integer 解析后生成的临时目录和临时文件的权限。 默认为0755 (owner rwx, group rx and others rx). CViewRenderer
isInitialized boolean 检查应用组件是否已经初始化。 CApplicationComponent
useRuntimePath boolean 是否将解析的结果保存在应用程序的runtime目录。 默认为true。 如果设置为false,解析结果将以文件形式保存在和源视图文件相同的目录下 文件名将会是源视图文件名加上字母c。 CViewRenderer

公共方法

隐藏继承方法

方法描述定义在
__call() 如果类中没有调的方法名,则调用这个方法。 CComponent
__get() 返回一个属性值、一个事件处理程序列表或一个行为名称。 CComponent
__isset() 检查一个属性是否为null。 CComponent
__set() 设置一个组件的属性值。 CComponent
__unset() 设置一个组件的属性为null。 CComponent
asa() 返回这个名字的行为对象。 CComponent
attachBehavior() 附加一个行为到组件。 CComponent
attachBehaviors() 附加一个行为列表到组件。 CComponent
attachEventHandler() 为事件附加一个事件处理程序。 CComponent
canGetProperty() 确定属性是否可读。 CComponent
canSetProperty() 确定属性是否可写。 CComponent
detachBehavior() 从组件中分离一个行为。 CComponent
detachBehaviors() 从组件中分离所有行为。 CComponent
detachEventHandler() 分离一个存在的事件处理程序。 CComponent
disableBehavior() 禁用一个附加行为。 CComponent
disableBehaviors() 禁用组件附加的所有行为。 CComponent
enableBehavior() 启用一个附加行为。 CComponent
enableBehaviors() 启用组件附加的所有行为。 CComponent
evaluateExpression() 计算一个PHP表达式,或根据组件上下文执行回调。 CComponent
getEventHandlers() 返回一个事件的附加处理程序列表。 CComponent
getIsInitialized() 检查应用组件是否已经初始化。 CApplicationComponent
hasEvent() 确定一个事件是否定义。 CComponent
hasEventHandler() 检查事件是否有附加的处理程序。 CComponent
hasProperty() 确定属性是否被定义。 CComponent
init() 初始化应用组件。 CApplicationComponent
raiseEvent() 发起一个事件。 CComponent
renderFile() 渲染一个视图文件。 CViewRenderer

受保护方法

隐藏继承方法

方法描述定义在
generateViewFile() 解析源视图文件,保存结果到另一个文件。 CPradoViewRenderer
getViewFile() 生成结果视图文件的路径。 CViewRenderer

方法详细

generateViewFile() 方法
protected void generateViewFile(string $sourceFile, string $viewFile)
$sourceFile string 源视图文件路径
$viewFile string 作为结果的视图文件路径
源码: framework/web/renderers/CPradoViewRenderer.php#81 (显示)
protected function generateViewFile($sourceFile,$viewFile)
{
    static 
$regexRules=array(
        
'<%=?\s*(.*?)\s*%>',        // PHP statements or expressions
        
'<\/?(com|cache|clip):([\w\.]+)\s*((?:\s*\w+\s*=\s*\'.*?(?<!\\\\)\'|\s*\w+\s*=\s*".*?(?<!\\\\)"|\s*\w+\s*=\s*\{.*?\})*)\s*\/?>'// component tags
        
'<!---.*?--->',    // template comments
    
);
    
$this->_sourceFile=$sourceFile;
    
$this->_input=file_get_contents($sourceFile);
    
$n=preg_match_all('/'.implode('|',$regexRules).'/msS',$this->_input,$matches,PREG_SET_ORDER|PREG_OFFSET_CAPTURE);
    
$textStart=0;
    
$this->_output="<?php /* source file: $sourceFile */ ?>\n";
    for(
$i=0;$i<$n;++$i)
    {
        
$match=&$matches[$i];
        
$str=$match[0][0];
        
$matchStart=$match[0][1];
        
$matchEnd=$matchStart+strlen($str)-1;

        if(
$matchStart>$textStart)
            
$this->_output.=substr($this->_input,$textStart,$matchStart-$textStart);
        
$textStart=$matchEnd+1;

        if(
strpos($str,'<com:')===0)    // opening component tag
        
{
            
$type=$match[3][0];
            if(
$str[strlen($str)-2]!=='/')  // open tag
                
$this->_output.=$this->processBeginWidget($type,$match[4][0],$match[2][1]);
            else
                
$this->_output.=$this->processWidget($type,$match[4][0],$match[2][1]);
        }
        else if(
strpos($str,'</com:')===0)    // closing component tag
            
$this->_output.=$this->processEndWidget($match[3][0],$match[2][1]);
        else if(
strpos($str,'<cache:')===0)    // opening cache tag
        
{
            
$id=$match[3][0];
            if(
$str[strlen($str)-2]!=='/')  // open tag
                
$this->_output.=$this->processBeginCache($id,$match[4][0],$match[2][1]);
            else
                
$this->_output.=$this->processCache($id,$match[4][0],$match[2][1]);
        }
        else if(
strpos($str,'</cache:')===0)    // closing cache tag
            
$this->_output.=$this->processEndCache($match[3][0],$match[2][1]);
        else if(
strpos($str,'<clip:')===0)    // opening clip tag
        
{
            
$id=$match[3][0];
            if(
$str[strlen($str)-2]!=='/')  // open tag
                
$this->_output.=$this->processBeginClip($id,$match[4][0],$match[2][1]);
            else
                
$this->_output.=$this->processClip($id,$match[4][0],$match[2][1]);
        }
        else if(
strpos($str,'</clip:')===0)    // closing clip tag
            
$this->_output.=$this->processEndClip($match[3][0],$match[2][1]);
        else if(
strpos($str,'<%=')===0)    // expression
            
$this->_output.=$this->processExpression($match[1][0],$match[1][1]);
        else if(
strpos($str,'<%')===0)    // statement
            
$this->_output.=$this->processStatement($match[1][0],$match[1][1]);
    }
    if(
$textStart<strlen($this->_input))
        
$this->_output.=substr($this->_input,$textStart);

    
file_put_contents($viewFile,$this->_output);
}

解析源视图文件,保存结果到另一个文件。 这个方法是父类所必须的。

Copyright © 2008-2011 by Yii Software LLC
All Rights Reserved.