0 follower

CClientScript

Package system.web
Inheritance class CClientScript » CApplicationComponent » CComponent
Implements IApplicationComponent
Since 1.0
Source Code framework/web/CClientScript.php
CClientScript manages JavaScript and CSS stylesheets for views.

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
behaviors array the behaviors that should be attached to this component. CApplicationComponent
corePackages array list of core script packages (name=>package spec). CClientScript
coreScriptPosition integer Where the scripts registered using registerCoreScript or registerPackage will be inserted in the page. CClientScript
coreScriptUrl string Returns the base URL of all core javascript files. CClientScript
defaultScriptFilePosition integer Where the scripts registered using registerScriptFile will be inserted in the page. CClientScript
defaultScriptPosition integer Where the scripts registered using registerScript will be inserted in the page. CClientScript
enableJavaScript boolean whether JavaScript should be enabled. CClientScript
isInitialized boolean Checks if this application component has been initialized. CApplicationComponent
packages array list of custom script packages (name=>package spec). CClientScript
scriptMap array the mapping between script file names and the corresponding script URLs. CClientScript
scripts array the registered JavaScript code blocks (position, key => code) CClientScript

Protected Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
coreScripts array the registered script packages (name => package spec) CClientScript
css array the registered css code blocks (key => array(CSS code, media type)). CClientScript
cssFiles array the registered CSS files (CSS URL=>media type). CClientScript
hasScripts boolean whether there are any javascript or css to be rendered. CClientScript
linkTags array the registered head link tags. CClientScript
metaTags array the registered head meta tags. CClientScript
scriptFiles array the registered JavaScript files (position, key => URL) CClientScript

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
addPackage() Adds a package to packages list. CClientScript
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
getCoreScriptUrl() Returns the base URL of all core javascript files. CClientScript
getEventHandlers() Returns the list of attached event handlers for an event. CComponent
getIsInitialized() Checks if this application component has been initialized. CApplicationComponent
getPackageBaseUrl() Returns the base URL for a registered package with the specified name. CClientScript
hasEvent() Determines whether an event is defined. CComponent
hasEventHandler() Checks whether the named event has attached handlers. CComponent
hasPackage() Checks if package is available. CClientScript
hasProperty() Determines whether a property is defined. CComponent
init() Initializes the application component. CApplicationComponent
isCssFileRegistered() Checks whether the CSS file has been registered. CClientScript
isCssRegistered() Checks whether the CSS code has been registered. CClientScript
isScriptFileRegistered() Checks whether the JavaScript file has been registered. CClientScript
isScriptRegistered() Checks whether the JavaScript code has been registered. CClientScript
raiseEvent() Raises an event. CComponent
registerCoreScript() Registers a script package that is listed in packages. CClientScript
registerCss() Registers a piece of CSS code. CClientScript
registerCssFile() Registers a CSS file CClientScript
registerLinkTag() Registers a link tag that will be inserted in the head section (right before the title element) of the resulting page. CClientScript
registerMetaTag() Registers a meta tag that will be inserted in the head section (right before the title element) of the resulting page. CClientScript
registerPackage() Registers a script package that is listed in packages. CClientScript
registerScript() Registers a piece of javascript code. CClientScript
registerScriptFile() Registers a javascript file. CClientScript
render() Renders the registered scripts. CClientScript
renderBodyBegin() Inserts the scripts at the beginning of the body section. CClientScript
renderBodyEnd() Inserts the scripts at the end of the body section. CClientScript
renderCoreScripts() Renders the specified core javascript library. CClientScript
renderHead() Inserts the scripts in the head section. CClientScript
reset() Cleans all registered scripts. CClientScript
setCoreScriptUrl() Sets the base URL of all core javascript files. CClientScript

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
recordCachingAction() Records a method call when an output cache is in effect. CClientScript
remapScripts() Uses scriptMap to re-map the registered scripts. CClientScript
renderScriptBatch() Composes script HTML block from the given script values, CClientScript
unifyScripts() Removes duplicated scripts from scriptFiles. CClientScript

Property Details

corePackages property (available since v1.1.7)
public array $corePackages;

list of core script packages (name=>package spec). Please refer to packages for details about package spec.

By default, the core script packages are specified in 'framework/web/js/packages.php'. You may configure this property to customize the core script packages.

When calling registerPackage to register a script package, packages will be checked first followed by this property. If a package is found, it will be registered for rendering later on.

coreScriptPosition property (available since v1.1.3)
public integer $coreScriptPosition;

Where the scripts registered using registerCoreScript or registerPackage will be inserted in the page. This can be one of the CClientScript::POS_* constants. Defaults to CClientScript::POS_HEAD.

coreScriptUrl property
public string getCoreScriptUrl()
public void setCoreScriptUrl(string $value)

Returns the base URL of all core javascript files. If the base URL is not explicitly set, this method will publish the whole directory 'framework/web/js/source' and return the corresponding URL.

coreScripts property (available since v1.1.7)
protected array $coreScripts;

the registered script packages (name => package spec)

css property (available since v1.1.3)
protected array $css;

the registered css code blocks (key => array(CSS code, media type)).

cssFiles property
protected array $cssFiles;

the registered CSS files (CSS URL=>media type).

defaultScriptFilePosition property (available since v1.1.11)
public integer $defaultScriptFilePosition;

Where the scripts registered using registerScriptFile will be inserted in the page. This can be one of the CClientScript::POS_* constants. Defaults to CClientScript::POS_HEAD.

defaultScriptPosition property (available since v1.1.11)
public integer $defaultScriptPosition;

Where the scripts registered using registerScript will be inserted in the page. This can be one of the CClientScript::POS_* constants. Defaults to CClientScript::POS_READY.

enableJavaScript property
public boolean $enableJavaScript;

whether JavaScript should be enabled. Defaults to true.

hasScripts property (available since v1.1.7)
protected boolean $hasScripts;

whether there are any javascript or css to be rendered.

linkTags property (available since v1.1.3)
protected array $linkTags;

the registered head link tags. Each array element represents an option array that will be passed as the last parameter of CHtml::linkTag.

metaTags property (available since v1.1.3)
protected array $metaTags;

the registered head meta tags. Each array element represents an option array that will be passed as the last parameter of CHtml::metaTag.

packages property (available since v1.1.7)
public array $packages;

list of custom script packages (name=>package spec). This property keeps a list of named script packages, each of which can contain a set of CSS and/or JavaScript script files, and their dependent package names. By calling registerPackage, one can register a whole package of client scripts together with their dependent packages and render them in the HTML output.

The array structure is as follows:

array(
  'package-name'=>array(
    'basePath'=>'alias of the directory containing the script files',
    'baseUrl'=>'base URL for the script files',
    'js'=>array(list of js files relative to basePath/baseUrl),
    'css'=>array(list of css files relative to basePath/baseUrl),
    'depends'=>array(list of dependent packages),
  ),
  ......
)


The JS and CSS files listed are relative to 'basePath'. For example, if 'basePath' is 'application.assets', a script named 'comments.js' will refer to the file 'protected/assets/comments.js'.

When a script is being rendered in HTML, it will be prefixed with 'baseUrl'. For example, if 'baseUrl' is '/assets', the 'comments.js' script will be rendered using URL '/assets/comments.js'.

If 'baseUrl' does not start with '/', the relative URL of the application entry script will be inserted at the beginning. For example, if 'baseUrl' is 'assets' and the current application runs with the URL 'http://localhost/demo/index.php', then the 'comments.js' script will be rendered using URL '/demo/assets/comments.js'.

If 'baseUrl' is not set, the script will be published by CAssetManager and the corresponding published URL will be used.

When calling registerPackage to register a script package, this property will be checked first followed by corePackages. If a package is found, it will be registered for rendering later on.
scriptFiles property
protected array $scriptFiles;

the registered JavaScript files (position, key => URL)

scriptMap property
public array $scriptMap;

the mapping between script file names and the corresponding script URLs. The array keys are script file names (without directory part) and the array values are the corresponding URLs. If an array value is false, the corresponding script file will not be rendered. If an array key is '*.js' or '*.css', the corresponding URL will replace all JavaScript files or CSS files, respectively.

This property is mainly used to optimize the generated HTML pages by merging different scripts files into fewer and optimized script files.

scripts property
public array $scripts;

the registered JavaScript code blocks (position, key => code)

Method Details

addPackage() method (available since v1.1.9)
public static addPackage(string $name, array $definition)
$name string the name of the script package.
$definition array the definition array of the script package,
{return} static the CClientScript object itself (to support method chaining, available since version 1.1.10).
Source Code: framework/web/CClientScript.php#871 (show)
public function addPackage($name,$definition)
{
    
$this->packages[$name]=$definition;
    return 
$this;
}

Adds a package to packages list.

See Also

  • CClientScript::packages.
getCoreScriptUrl() method
public string getCoreScriptUrl()
{return} string the base URL of all core javascript files
Source Code: framework/web/CClientScript.php#506 (show)
public function getCoreScriptUrl()
{
    if(
$this->_baseUrl!==null)
        return 
$this->_baseUrl;
    else
        return 
$this->_baseUrl=Yii::app()->getAssetManager()->publish(YII_PATH.'/web/js/source');
}

Returns the base URL of all core javascript files. If the base URL is not explicitly set, this method will publish the whole directory 'framework/web/js/source' and return the corresponding URL.

getPackageBaseUrl() method (available since v1.1.8)
public string getPackageBaseUrl(string $name)
$name string the package name
{return} string the base URL for the named package. False is returned if the package is not registered yet.
Source Code: framework/web/CClientScript.php#533 (show)
public function getPackageBaseUrl($name)
{
    if(!isset(
$this->coreScripts[$name]))
        return 
false;
    
$package=$this->coreScripts[$name];
    if(isset(
$package['baseUrl']))
    {
        
$baseUrl=$package['baseUrl'];
        if(
$baseUrl==='' || $baseUrl[0]!=='/' && strpos($baseUrl,'://')===false)
            
$baseUrl=Yii::app()->getRequest()->getBaseUrl().'/'.$baseUrl;
        
$baseUrl=rtrim($baseUrl,'/');
    }
    elseif(isset(
$package['basePath']))
        
$baseUrl=Yii::app()->getAssetManager()->publish(Yii::getPathOfAlias($package['basePath']));
    else
        
$baseUrl=$this->getCoreScriptUrl();

    return 
$this->coreScripts[$name]['baseUrl']=$baseUrl;
}

Returns the base URL for a registered package with the specified name. If needed, this method may publish the assets of the package and returns the published base URL.

See Also

hasPackage() method (available since v1.1.18)
public bool hasPackage(string $name)
$name string the name of the script package.
{return} bool
Source Code: framework/web/CClientScript.php#560 (show)
public function hasPackage($name)
{
    if(isset(
$this->coreScripts[$name]))
        return 
true;
    if(isset(
$this->packages[$name]))
        return 
true;
    else
    {
        if(
$this->corePackages===null)
            
$this->corePackages=require(YII_PATH.'/web/js/packages.php');
        if(isset(
$this->corePackages[$name]))
            return 
true;
    }
    return 
false;
}

Checks if package is available.

See Also

isCssFileRegistered() method
public boolean isCssFileRegistered(string $url)
$url string URL of the CSS file
{return} boolean whether the CSS file is already registered
Source Code: framework/web/CClientScript.php#796 (show)
public function isCssFileRegistered($url)
{
    return isset(
$this->cssFiles[$url]);
}

Checks whether the CSS file has been registered.

isCssRegistered() method
public boolean isCssRegistered(string $id)
$id string ID that uniquely identifies the CSS code
{return} boolean whether the CSS code is already registered
Source Code: framework/web/CClientScript.php#806 (show)
public function isCssRegistered($id)
{
    return isset(
$this->css[$id]);
}

Checks whether the CSS code has been registered.

isScriptFileRegistered() method
public boolean isScriptFileRegistered(string $url, integer $position=0)
$url string URL of the javascript file
$position integer the position of the JavaScript code. Valid values include the following:
  • CClientScript::POS_HEAD : the script is inserted in the head section right before the title element.
  • CClientScript::POS_BEGIN : the script is inserted at the beginning of the body section.
  • CClientScript::POS_END : the script is inserted at the end of the body section.
{return} boolean whether the javascript file is already registered
Source Code: framework/web/CClientScript.php#822 (show)
public function isScriptFileRegistered($url,$position=self::POS_HEAD)
{
    return isset(
$this->scriptFiles[$position][$url]);
}

Checks whether the JavaScript file has been registered.

isScriptRegistered() method
public boolean isScriptRegistered(string $id, integer $position=4)
$id string ID that uniquely identifies the JavaScript code
$position integer the position of the JavaScript code. Valid values include the following:
  • CClientScript::POS_HEAD : the script is inserted in the head section right before the title element.
  • CClientScript::POS_BEGIN : the script is inserted at the beginning of the body section.
  • CClientScript::POS_END : the script is inserted at the end of the body section.
  • CClientScript::POS_LOAD : the script is inserted in the window.onload() function.
  • CClientScript::POS_READY : the script is inserted in the jQuery's ready function.
{return} boolean whether the javascript code is already registered
Source Code: framework/web/CClientScript.php#840 (show)
public function isScriptRegistered($id,$position=self::POS_READY)
{
    return isset(
$this->scripts[$position][$id]);
}

Checks whether the JavaScript code has been registered.

recordCachingAction() method
protected void recordCachingAction(string $context, string $method, array $params)
$context string a property name of the controller. It refers to an object whose method is being called. If empty it means the controller itself.
$method string the method name
$params array parameters passed to the method
Source Code: framework/web/CClientScript.php#855 (show)
protected function recordCachingAction($context,$method,$params)
{
    if((
$controller=Yii::app()->getController())!==null)
        
$controller->recordCachingAction($context,$method,$params);
}

Records a method call when an output cache is in effect. This is a shortcut to Yii::app()->controller->recordCachingAction. In case when controller is absent, nothing is recorded.

See Also

registerCoreScript() method
public static registerCoreScript(string $name)
$name string the name of the script package.
{return} static the CClientScript object itself (to support method chaining, available since version 1.1.5).
Source Code: framework/web/CClientScript.php#596 (show)
public function registerCoreScript($name)
{
    if(isset(
$this->coreScripts[$name]))
        return 
$this;
    if(isset(
$this->packages[$name]))
        
$package=$this->packages[$name];
    else
    {
        if(
$this->corePackages===null)
            
$this->corePackages=require(YII_PATH.'/web/js/packages.php');
        if(isset(
$this->corePackages[$name]))
            
$package=$this->corePackages[$name];
    }
    if(isset(
$package))
    {
        if(!empty(
$package['depends']))
        {
            foreach(
$package['depends'] as $p)
                
$this->registerCoreScript($p);
        }
        
$this->coreScripts[$name]=$package;
        
$this->hasScripts=true;
        
$params=func_get_args();
        
$this->recordCachingAction('clientScript','registerCoreScript',$params);
    }
    elseif(
YII_DEBUG)
        throw new 
CException('There is no CClientScript package: '.$name);
    else
        
Yii::log('There is no CClientScript package: '.$name,CLogger::LEVEL_WARNING,'system.web.CClientScript');

    return 
$this;
}

Registers a script package that is listed in packages.

See Also

  • renderCoreScript
registerCss() method
public static registerCss(string $id, string $css, string $media='')
$id string ID that uniquely identifies this piece of CSS code
$css string the CSS code
$media string media that the CSS code should be applied to. If empty, it means all media types.
{return} static the CClientScript object itself (to support method chaining, available since version 1.1.5).
Source Code: framework/web/CClientScript.php#651 (show)
public function registerCss($id,$css,$media='')
{
    
$this->hasScripts=true;
    
$this->css[$id]=array($css,$media);
    
$params=func_get_args();
    
$this->recordCachingAction('clientScript','registerCss',$params);
    return 
$this;
}

Registers a piece of CSS code.

registerCssFile() method
public static registerCssFile(string $url, string $media='')
$url string URL of the CSS file
$media string media that the CSS file should be applied to. If empty, it means all media types.
{return} static the CClientScript object itself (to support method chaining, available since version 1.1.5).
Source Code: framework/web/CClientScript.php#635 (show)
public function registerCssFile($url,$media='')
{
    
$this->hasScripts=true;
    
$this->cssFiles[$url]=$media;
    
$params=func_get_args();
    
$this->recordCachingAction('clientScript','registerCssFile',$params);
    return 
$this;
}

Registers a CSS file

registerLinkTag() method
public static registerLinkTag(string $relation=NULL, string $type=NULL, string $href=NULL, string $media=NULL, array $options=array ( ))
$relation string rel attribute of the link tag. If null, the attribute will not be generated.
$type string type attribute of the link tag. If null, the attribute will not be generated.
$href string href attribute of the link tag. If null, the attribute will not be generated.
$media string media attribute of the link tag. If null, the attribute will not be generated.
$options array other options in name-value pairs
{return} static the CClientScript object itself (to support method chaining, available since version 1.1.5).
Source Code: framework/web/CClientScript.php#773 (show)
public function registerLinkTag($relation=null,$type=null,$href=null,$media=null,$options=array())
{
    
$params=func_get_args();

    
$this->hasScripts=true;
    if(
$relation!==null)
        
$options['rel']=$relation;
    if(
$type!==null)
        
$options['type']=$type;
    if(
$href!==null)
        
$options['href']=$href;
    if(
$media!==null)
        
$options['media']=$media;
    
$this->linkTags[serialize($options)]=$options;
    
$this->recordCachingAction('clientScript','registerLinkTag',$params);
    return 
$this;
}

Registers a link tag that will be inserted in the head section (right before the title element) of the resulting page.

registerMetaTag() method
public static registerMetaTag(string $content, string $name=NULL, string $httpEquiv=NULL, array $options=array ( ), string $id=NULL)
$content string content attribute of the meta tag
$name string name attribute of the meta tag. If null, the attribute will not be generated
$httpEquiv string http-equiv attribute of the meta tag. If null, the attribute will not be generated
$options array other options in name-value pairs (e.g. 'scheme', 'lang')
$id string Optional id of the meta tag to avoid duplicates
{return} static the CClientScript object itself (to support method chaining, available since version 1.1.5).
Source Code: framework/web/CClientScript.php#749 (show)
public function registerMetaTag($content,$name=null,$httpEquiv=null,$options=array(),$id=null)
{
    
$params=func_get_args();

    
$this->hasScripts=true;
    if(
$name!==null)
        
$options['name']=$name;
    if(
$httpEquiv!==null)
        
$options['http-equiv']=$httpEquiv;
    
$options['content']=$content;
    
$this->metaTags[null===$id?count($this->metaTags):$id]=$options;
    
$this->recordCachingAction('clientScript','registerMetaTag',$params);
    return 
$this;
}

Registers a meta tag that will be inserted in the head section (right before the title element) of the resulting page.

Note: Each call of this method will cause a rendering of new meta tag, even if their attributes are equal.

Example:

   $cs->registerMetaTag('example', 'description', null, array('lang' => 'en'));
   $cs->registerMetaTag('beispiel', 'description', null, array('lang' => 'de'));
registerPackage() method (available since v1.1.7)
public static registerPackage(string $name)
$name string the name of the script package.
{return} static the CClientScript object itself (to support method chaining, available since version 1.1.5).
Source Code: framework/web/CClientScript.php#584 (show)
public function registerPackage($name)
{
    return 
$this->registerCoreScript($name);
}

Registers a script package that is listed in packages. This method is the same as registerCoreScript.

See Also

  • renderCoreScript
registerScript() method
public static registerScript(string $id, string $script, integer $position=NULL, array $htmlOptions=array ( ))
$id string ID that uniquely identifies this piece of JavaScript code
$script string the javascript code
$position integer the position of the JavaScript code. Valid values include the following:
  • CClientScript::POS_HEAD : the script is inserted in the head section right before the title element.
  • CClientScript::POS_BEGIN : the script is inserted at the beginning of the body section.
  • CClientScript::POS_END : the script is inserted at the end of the body section.
  • CClientScript::POS_LOAD : the script is inserted in the window.onload() function.
  • CClientScript::POS_READY : the script is inserted in the jQuery's ready function.
$htmlOptions array additional HTML attributes Note: HTML attributes are not allowed for script positions "CClientScript::POS_LOAD" and "CClientScript::POS_READY".
{return} static the CClientScript object itself (to support method chaining, available since version 1.1.5).
Source Code: framework/web/CClientScript.php#708 (show)
public function registerScript($id,$script,$position=null,array $htmlOptions=array())
{
    
$params=func_get_args();

    if(
$position===null)
        
$position=$this->defaultScriptPosition;
    
$this->hasScripts=true;
    if(empty(
$htmlOptions))
        
$scriptValue=$script;
    else
    {
        if(
$position==self::POS_LOAD || $position==self::POS_READY)
            throw new 
CException(Yii::t('yii','Script HTML options are not allowed for "CClientScript::POS_LOAD" and "CClientScript::POS_READY".'));
        
$scriptValue=$htmlOptions;
        
$scriptValue['content']=$script;
    }
    
$this->scripts[$position][$id]=$scriptValue;
    if(
$position===self::POS_READY || $position===self::POS_LOAD)
        
$this->registerCoreScript('jquery');
    
$this->recordCachingAction('clientScript','registerScript',$params);
    return 
$this;
}

Registers a piece of javascript code.

registerScriptFile() method
public static registerScriptFile(string $url, integer $position=NULL, array $htmlOptions=array ( ))
$url string URL of the javascript file
$position integer the position of the JavaScript code. Valid values include the following:
  • CClientScript::POS_HEAD : the script is inserted in the head section right before the title element.
  • CClientScript::POS_BEGIN : the script is inserted at the beginning of the body section.
  • CClientScript::POS_END : the script is inserted at the end of the body section.
$htmlOptions array additional HTML attributes
{return} static the CClientScript object itself (to support method chaining, available since version 1.1.5).
Source Code: framework/web/CClientScript.php#672 (show)
public function registerScriptFile($url,$position=null,array $htmlOptions=array())
{
    
$params=func_get_args();

    if(
$position===null)
        
$position=$this->defaultScriptFilePosition;
    
$this->hasScripts=true;
    if(empty(
$htmlOptions))
        
$value=$url;
    else
    {
        
$value=$htmlOptions;
        
$value['src']=$url;
    }
    
$this->scriptFiles[$position][$url]=$value;
    
$this->recordCachingAction('clientScript','registerScriptFile',$params);
    return 
$this;
}

Registers a javascript file.

remapScripts() method
protected void remapScripts()
Source Code: framework/web/CClientScript.php#259 (show)
protected function remapScripts()
{
    
$cssFiles=array();
    foreach(
$this->cssFiles as $url=>$media)
    {
        
$name=basename($url);
        if(isset(
$this->scriptMap[$name]))
        {
            if(
$this->scriptMap[$name]!==false)
                
$cssFiles[$this->scriptMap[$name]]=$media;
        }
        elseif(isset(
$this->scriptMap['*.css']))
        {
            if(
$this->scriptMap['*.css']!==false)
                
$cssFiles[$this->scriptMap['*.css']]=$media;
        }
        else
            
$cssFiles[$url]=$media;
    }
    
$this->cssFiles=$cssFiles;

    
$jsFiles=array();
    foreach(
$this->scriptFiles as $position=>$scriptFiles)
    {
        
$jsFiles[$position]=array();
        foreach(
$scriptFiles as $scriptFile=>$scriptFileValue)
        {
            
$name=basename($scriptFile);
            if(isset(
$this->scriptMap[$name]))
            {
                if(
$this->scriptMap[$name]!==false)
                    
$jsFiles[$position][$this->scriptMap[$name]]=$this->scriptMap[$name];
            }
            elseif(isset(
$this->scriptMap['*.js']))
            {
                if(
$this->scriptMap['*.js']!==false)
                    
$jsFiles[$position][$this->scriptMap['*.js']]=$this->scriptMap['*.js'];
            }
            else
                
$jsFiles[$position][$scriptFile]=$scriptFileValue;
        }
    }
    
$this->scriptFiles=$jsFiles;
}

Uses scriptMap to re-map the registered scripts.

render() method
public void render(string &$output)
$output string the existing output that needs to be inserted with script tags
Source Code: framework/web/CClientScript.php#203 (show)
public function render(&$output)
{
    if(!
$this->hasScripts)
        return;

    
$this->renderCoreScripts();

    if(!empty(
$this->scriptMap))
        
$this->remapScripts();

    
$this->unifyScripts();

    
$this->renderHead($output);
    if(
$this->enableJavaScript)
    {
        
$this->renderBodyBegin($output);
        
$this->renderBodyEnd($output);
    }
}

Renders the registered scripts. This method is called in CController::render when it finishes rendering content. CClientScript thus gets a chance to insert script tags at head and body sections in the HTML output.

renderBodyBegin() method
public void renderBodyBegin(string &$output)
$output string the output to be inserted with scripts.
Source Code: framework/web/CClientScript.php#426 (show)
public function renderBodyBegin(&$output)
{
    
$html='';
    if(isset(
$this->scriptFiles[self::POS_BEGIN]))
    {
        foreach(
$this->scriptFiles[self::POS_BEGIN] as $scriptFileUrl=>$scriptFileValue)
        {
            if(
is_array($scriptFileValue))
                
$html.=CHtml::scriptFile($scriptFileUrl,$scriptFileValue)."\n";
            else
                
$html.=CHtml::scriptFile($scriptFileUrl)."\n";
        }
    }
    if(isset(
$this->scripts[self::POS_BEGIN]))
        
$html.=$this->renderScriptBatch($this->scripts[self::POS_BEGIN]);

    if(
$html!=='')
    {
        
$count=0;
        
$output=preg_replace('/(<body\b[^>]*>)/is','$1<###begin###>',$output,1,$count);
        if(
$count)
            
$output=str_replace('<###begin###>',$html,$output);
        else
            
$output=$html.$output;
    }
}

Inserts the scripts at the beginning of the body section.

renderBodyEnd() method
public void renderBodyEnd(string &$output)
$output string the output to be inserted with scripts.
Source Code: framework/web/CClientScript.php#457 (show)
public function renderBodyEnd(&$output)
{
    if(!isset(
$this->scriptFiles[self::POS_END]) && !isset($this->scripts[self::POS_END])
        && !isset(
$this->scripts[self::POS_READY]) && !isset($this->scripts[self::POS_LOAD]))
        return;

    
$fullPage=0;
    
$output=preg_replace('/(<\\/body\s*>)/is','<###end###>$1',$output,1,$fullPage);
    
$html='';
    if(isset(
$this->scriptFiles[self::POS_END]))
    {
        foreach(
$this->scriptFiles[self::POS_END] as $scriptFileUrl=>$scriptFileValue)
        {
            if(
is_array($scriptFileValue))
                
$html.=CHtml::scriptFile($scriptFileUrl,$scriptFileValue)."\n";
            else
                
$html.=CHtml::scriptFile($scriptFileUrl)."\n";
        }
    }
    
$scripts=isset($this->scripts[self::POS_END]) ? $this->scripts[self::POS_END] : array();
    if(isset(
$this->scripts[self::POS_READY]))
    {
        if(
$fullPage)
            
$scripts[]="jQuery(function($) {\n".implode("\n",$this->scripts[self::POS_READY])."\n});";
        else
            
$scripts[]=implode("\n",$this->scripts[self::POS_READY]);
    }
    if(isset(
$this->scripts[self::POS_LOAD]))
    {
        if(
$fullPage)
            
$scripts[]="jQuery(window).on('load',function() {\n".implode("\n",$this->scripts[self::POS_LOAD])."\n});";
        else
            
$scripts[]=implode("\n",$this->scripts[self::POS_LOAD]);
    }
    if(!empty(
$scripts))
        
$html.=$this->renderScriptBatch($scripts);

    if(
$fullPage)
        
$output=str_replace('<###end###>',$html,$output);
    else
        
$output=$output.$html;
}

Inserts the scripts at the end of the body section.

renderCoreScripts() method
public void renderCoreScripts()
Source Code: framework/web/CClientScript.php#341 (show)
public function renderCoreScripts()
{
    if(
$this->coreScripts===null)
        return;
    
$cssFiles=array();
    
$jsFiles=array();
    foreach(
$this->coreScripts as $name=>$package)
    {
        
$baseUrl=$this->getPackageBaseUrl($name);
        if(!empty(
$package['js']))
        {
            foreach(
$package['js'] as $js)
                
$jsFiles[$baseUrl.'/'.$js]=$baseUrl.'/'.$js;
        }
        if(!empty(
$package['css']))
        {
            foreach(
$package['css'] as $css)
                
$cssFiles[$baseUrl.'/'.$css]='';
        }
    }
    
// merge in place
    
if($cssFiles!==array())
    {
        foreach(
$this->cssFiles as $cssFile=>$media)
            
$cssFiles[$cssFile]=$media;
        
$this->cssFiles=$cssFiles;
    }
    if(
$jsFiles!==array())
    {
        if(isset(
$this->scriptFiles[$this->coreScriptPosition]))
        {
            foreach(
$this->scriptFiles[$this->coreScriptPosition] as $url => $value)
                
$jsFiles[$url]=$value;
        }
        
$this->scriptFiles[$this->coreScriptPosition]=$jsFiles;
    }
}

Renders the specified core javascript library.

renderHead() method
public void renderHead(string &$output)
$output string the output to be inserted with scripts.
Source Code: framework/web/CClientScript.php#383 (show)
public function renderHead(&$output)
{
    
$html='';
    foreach(
$this->metaTags as $meta)
        
$html.=CHtml::metaTag($meta['content'],null,null,$meta)."\n";
    foreach(
$this->linkTags as $link)
        
$html.=CHtml::linkTag(null,null,null,null,$link)."\n";
    foreach(
$this->cssFiles as $url=>$media)
        
$html.=CHtml::cssFile($url,$media)."\n";
    foreach(
$this->css as $css)
        
$html.=CHtml::css($css[0],$css[1])."\n";
    if(
$this->enableJavaScript)
    {
        if(isset(
$this->scriptFiles[self::POS_HEAD]))
        {
            foreach(
$this->scriptFiles[self::POS_HEAD] as $scriptFileValueUrl=>$scriptFileValue)
            {
                if(
is_array($scriptFileValue))
                    
$html.=CHtml::scriptFile($scriptFileValueUrl,$scriptFileValue)."\n";
                else
                    
$html.=CHtml::scriptFile($scriptFileValueUrl)."\n";
            }
        }

        if(isset(
$this->scripts[self::POS_HEAD]))
            
$html.=$this->renderScriptBatch($this->scripts[self::POS_HEAD]);
    }

    if(
$html!=='')
    {
        
$count=0;
        
$output=preg_replace('/(<title\b[^>]*>|<\\/head\s*>)/is','<###head###>$1',$output,1,$count);
        if(
$count)
            
$output=str_replace('<###head###>',$html,$output);
        else
            
$output=$html.$output;
    }
}

Inserts the scripts in the head section.

renderScriptBatch() method
protected string renderScriptBatch(array $scripts)
$scripts array script values to process.
{return} string HTML output
Source Code: framework/web/CClientScript.php#310 (show)
protected function renderScriptBatch(array $scripts)
{
    
$html '';
    
$scriptBatches = array();
    foreach(
$scripts as $scriptValue)
    {
        if(
is_array($scriptValue))
        {
            
$scriptContent $scriptValue['content'];
            unset(
$scriptValue['content']);
            
$scriptHtmlOptions $scriptValue;
            
ksort($scriptHtmlOptions);
        }
        else
        {
            
$scriptContent $scriptValue;
            
$scriptHtmlOptions = array();
        }
        
$key=serialize($scriptHtmlOptions);
        
$scriptBatches[$key]['htmlOptions']=$scriptHtmlOptions;
        
$scriptBatches[$key]['scripts'][]=$scriptContent;
    }
    foreach(
$scriptBatches as $scriptBatch)
        if(!empty(
$scriptBatch['scripts']))
            
$html.=CHtml::script(implode("\n",$scriptBatch['scripts']),$scriptBatch['htmlOptions'])."\n";
    return 
$html;
}

Composes script HTML block from the given script values, attempting to group scripts at single 'script' tag if possible.

reset() method
public void reset()
Source Code: framework/web/CClientScript.php#182 (show)
public function reset()
{
    
$this->hasScripts=false;
    
$this->coreScripts=array();
    
$this->cssFiles=array();
    
$this->css=array();
    
$this->scriptFiles=array();
    
$this->scripts=array();
    
$this->metaTags=array();
    
$this->linkTags=array();

    
$this->recordCachingAction('clientScript','reset',array());
}

Cleans all registered scripts.

setCoreScriptUrl() method
public void setCoreScriptUrl(string $value)
$value string the base URL of all core javascript files.
Source Code: framework/web/CClientScript.php#520 (show)
public function setCoreScriptUrl($value)
{
    
$this->_baseUrl=$value;
}

Sets the base URL of all core javascript files. This setter is provided in case when core javascript files are manually published to a pre-specified location. This may save asset publishing time for large-scale applications.

unifyScripts() method (available since v1.1.5)
protected void unifyScripts()
Source Code: framework/web/CClientScript.php#227 (show)
protected function unifyScripts()
{
    if(!
$this->enableJavaScript)
        return;
    
$map=array();
    if(isset(
$this->scriptFiles[self::POS_HEAD]))
        
$map=$this->scriptFiles[self::POS_HEAD];

    if(isset(
$this->scriptFiles[self::POS_BEGIN]))
    {
        foreach(
$this->scriptFiles[self::POS_BEGIN] as $scriptFile=>$scriptFileValue)
        {
            if(isset(
$map[$scriptFile]))
                unset(
$this->scriptFiles[self::POS_BEGIN][$scriptFile]);
            else
                
$map[$scriptFile]=true;
        }
    }

    if(isset(
$this->scriptFiles[self::POS_END]))
    {
        foreach(
$this->scriptFiles[self::POS_END] as $key=>$scriptFile)
        {
            if(isset(
$map[$key]))
                unset(
$this->scriptFiles[self::POS_END][$key]);
        }
    }
}

Removes duplicated scripts from scriptFiles.