0 follower

CViewRenderer

Package system.web.renderers
Inheritance abstract class CViewRenderer » CApplicationComponent » CComponent
Implements IApplicationComponent, IViewRenderer
Subclasses CPradoViewRenderer
Since 1.0
Source Code framework/web/renderers/CViewRenderer.php
CViewRenderer is the base class for view renderer classes.

A view renderer is an application component that renders views written in a customized syntax.

Once installing a view renderer as a 'viewRenderer' application component, the normal view rendering process will be intercepted by the renderer. The renderer will first parse the source view file and then render the the resulting view file.

Parsing results are saved as temporary files that may be stored under the application runtime directory or together with the source view file.

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 Checks if this application component has been initialized. 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
evaluateExpression() Evaluates a PHP expression or callback under the context of this component. CComponent
getEventHandlers() Returns the list of attached event handlers for an event. CComponent
getIsInitialized() Checks if this application component has been initialized. 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. CViewRenderer
getViewFile() Generates the resulting view file path. CViewRenderer

Property Details

fileExtension property
public string $fileExtension;

the extension name of the view file. Defaults to '.php'.

filePermission property
public integer $filePermission;

the chmod permission for temporary directories and files generated during parsing. Defaults to 0755 (owner rwx, group rx and others rx).

useRuntimePath property
public boolean $useRuntimePath;

whether to store the parsing results in the application's runtime directory. Defaults to true. If false, the parsing results will be saved as files under the same directory as the source view files and the file names will be the source file names appended with letter 'c'.

Method Details

generateViewFile() method
abstract 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/CViewRenderer.php#54 (show)
abstract protected function generateViewFile($sourceFile,$viewFile);

Parses the source view file and saves the results as another file.

getViewFile() method
protected string getViewFile(string $file)
$file string source view file path
{return} string resulting view file path
Source Code: framework/web/renderers/CViewRenderer.php#84 (show)
protected function getViewFile($file)
{
    if(
$this->useRuntimePath)
    {
        
$crc=sprintf('%x'crc32(get_class($this).Yii::getVersion().dirname($file)));
        
$viewFile=Yii::app()->getRuntimePath().DIRECTORY_SEPARATOR.'views'.DIRECTORY_SEPARATOR.$crc.DIRECTORY_SEPARATOR.basename($file);
        if(!
is_file($viewFile))
            @
mkdir(dirname($viewFile),$this->filePermission,true);
        return 
$viewFile;
    }
    else
        return 
$file.'c';
}

Generates the resulting view file path.

renderFile() method
public mixed renderFile(CBaseController $context, string $sourceFile, mixed $data, boolean $return)
$context CBaseController the controller or widget who is rendering the view file.
$sourceFile string the view file path
$data mixed the data to be passed to the view
$return boolean whether the rendering result should be returned
{return} mixed the rendering result, or null if the rendering result is not needed.
Source Code: framework/web/renderers/CViewRenderer.php#66 (show)
public function renderFile($context,$sourceFile,$data,$return)
{
    if(!
is_file($sourceFile) || ($file=realpath($sourceFile))===false)
        throw new 
CException(Yii::t('yii','View file "{file}" does not exist.',array('{file}'=>$sourceFile)));
    
$viewFile=$this->getViewFile($sourceFile);
    if(@
filemtime($sourceFile)>@filemtime($viewFile))
    {
        
$this->generateViewFile($sourceFile,$viewFile);
        @
chmod($viewFile,$this->filePermission);
    }
    return 
$context->renderInternal($viewFile,$data,$return);
}

Renders a view file. This method is required by IViewRenderer.