Skip to content

Instantly share code, notes, and snippets.

@MGHollander
Last active June 12, 2020 08:14
Show Gist options
  • Save MGHollander/c3f677260f742e6cacce to your computer and use it in GitHub Desktop.
Save MGHollander/c3f677260f742e6cacce to your computer and use it in GitHub Desktop.
Yii1.1 CClientScript extension to highlight JavaScript code block and capture them with the registerScript function (Yii2 version: https://gist.github.com/MGHollander/fe913a62efadc3bc5d9d123f7a7f870d)
<?php
/**
* ClientScript class file.
*
* @author Tsunu {@link http://www.yiiframework.com/forum/index.php/topic/24429-the-best-way-to-write-javascript/page__p__118436#entry118436}
* @author MGHollander {@link https://github.com/MGHollander}
*/
/**
* ClientScript class is an extension on Yii's CClientScript to register scripts inside a view.
* It keeps your code hightlighted and readable by your IDE.
*
* I've modified the code to remove the script tags using a regular expression, because it may occur that other HTML tags are used within a JavaScript string. I also add a parameter for htmlOptions since this is available in the registerScript function. Finally I added some comments.
*
* Usage:
* 1. Save the file in your /proteced/components/ dir
* 2. Add the classname to your config
*
* return array(
* ...
* 'components'=>array(
* 'clientScript'=>array(
* 'class'=>'ClientScript',
* ...
* ),
* ...
* ),
* ...
* );
*
* Usage is simular to {@link CClientScript::registerScript}
* The <script> tags are required for highlighting and IDE functionallity and will be removed automatically.
*
* Example:
*
* <?php $cs = Yii::app()->clientScript;
* $cs->beginScript('my-first-script', CClientScript::POS_HEAD, array('charset'=>'UTF-8')); ?>
* <script>
* // Write your code here.
* </script>
* <?php $cs->endScript(); ?>
*
* <?php $cs->beginScript('my-second-script'); ?>
* <script type="text/javascript">
* // Write your code here.
* </script>
* <?php $cs->endScript(); ?>
*/
class ClientScript extends CClientScript {
/**
* @see {@link CClientScript::registerScript} id parameter
* @var string
*/
protected $id;
/**
* @see {@link CClientScript::registerScript} position parameter
* @var integer
*/
protected $position;
/**
* @see {@link CClientScript::registerScript} htmlOptions parameter
* @var array
*/
protected $htmlOptions;
/**
* Set attributes and turn on output buffering.
*/
public function beginScript($id, $position = null, $htmlOptions = array()) {
$this->id = $id;
$this->position = $position;
$this->htmlOptions = $htmlOptions;
ob_start();
ob_implicit_flush(false);
}
/**
* Get default position if non set, get current buffer contents, delete current output buffer and register the contents.
* @see {@link CClientScript::registerScript} return description
*/
public function endScript() {
parent::registerScript($this->id, preg_replace('/\s*<\/?script(.*)>\s*/i', '', ob_get_clean()), $this->position, $this->htmlOptions);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment