0 follower

CPradoViewRenderer

Package system.web.renderers
Inheritance class CPradoViewRenderer » CViewRenderer » CApplicationComponent » CComponent
Implements IViewRenderer, IApplicationComponent
Since 1.0
Version $Id$
Source Code framework/web/renderers/CPradoViewRenderer.php
CPradoViewRenderer implements a view renderer that allows users to use a template syntax similar to PRADO templates.

To use CPradoViewRenderer, configure it as an application component named "viewRenderer" in the application configuration:
array(
    'components'=>array(
        ......
        'viewRenderer'=>array(
            'class'=>'CPradoViewRenderer',
        ),
    ),
)


CPradoViewRenderer allows you to write view files with the following syntax:
// PHP tags:
<%= expression %>
// <?php echo expression ?>
<% statement %>
// &lt;?php statement ?&gt;</li>

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

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

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

// comment tags:
&lt;!--- comments ---&gt;
// the whole tag will be stripped off

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
behaviors array the behaviors that should be attached to this component. CApplicationComponent
fileExtension string the extension name of the view file. CViewRenderer
filePermission integer the chmod permission for temporary directories and files generated during parsing. CViewRenderer
isInitialized boolean whether this application component has been initialized (i.e., init() is invoked. CApplicationComponent
useRuntimePath boolean whether to store the parsing results in the application's runtime directory. CViewRenderer

Public Methods

Hide inherited methods

MethodDescriptionDefined By
__call() Calls the named method which is not a class method. CComponent
__get() Returns a property value, an event handler list or a behavior based on its name. CComponent
__isset() Checks if a property value is null. CComponent
__set() Sets value of a component property. CComponent
__unset() Sets a component property to be null. CComponent
asa() Returns the named behavior object. CComponent
attachBehavior() Attaches a behavior to this component. CComponent
attachBehaviors() Attaches a list of behaviors to the component. CComponent
attachEventHandler() Attaches an event handler to an event. CComponent
canGetProperty() Determines whether a property can be read. CComponent
canSetProperty() Determines whether a property can be set. CComponent
detachBehavior() Detaches a behavior from the component. CComponent
detachBehaviors() Detaches all behaviors from the component. CComponent
detachEventHandler() Detaches an existing event handler. CComponent
disableBehavior() Disables an attached behavior. CComponent
disableBehaviors() Disables all behaviors attached to this component. CComponent
enableBehavior() Enables an attached behavior. CComponent
enableBehaviors() Enables all behaviors attached to this component. CComponent
getEventHandlers() Returns the list of attached event handlers for an event. CComponent
getIsInitialized() Checks whether this application component has been initialized (i.e., init() is invoked.) CApplicationComponent
hasEvent() Determines whether an event is defined. CComponent
hasEventHandler() Checks whether the named event has attached handlers. CComponent
hasProperty() Determines whether a property is defined. CComponent
init() Initializes the application component. CApplicationComponent
raiseEvent() Raises an event. CComponent
renderFile() Renders a view file. CViewRenderer

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
generateViewFile() Parses the source view file and saves the results as another file. CPradoViewRenderer
getViewFile() Generates the resulting view file path. CViewRenderer

Method Details

generateViewFile() method
protected void generateViewFile(string $sourceFile, string $viewFile)
$sourceFile string the source view file path
$viewFile string the resulting view file path
Source Code: framework/web/renderers/CPradoViewRenderer.php#81 (show)
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);
}

Parses the source view file and saves the results as another file. This method is required by the parent class.