CTextHighlighter用來格式化顯示代碼,目前支持顯示的語言有ABAP, CPP, CSS, DIFF, DTD, HTML, JAVA, JAVASCRIPT, MYSQL, PERL, PHP, PYTHON, RUBY, SQL, XML,顯示代碼時也可以顯示行號,通過showLineNumbers=TRUE打開行號顯示。
本例顯示PHP代碼,一個帶行號,一個不帶行號:
PHP code <!--?php $this--->beginWidget('CTextHighlighter',array('language'=>'PHP')); ?> // include Yii bootstrap file //require_once(dirname(__FILE__).'/../../framework/yii.php'); $yii='C:/yiiframework/yii.php'; // remove the following line when in production mode defined('YII_DEBUG') or define('YII_DEBUG',true); $config=dirname(__FILE__).'/protected/config/main.php'; // remove the following line when in production mode // defined('YII_DEBUG') or define('YII_DEBUG',true); require_once($yii); Yii::createWebApplication($config)->run(); <!--?php $this--->endWidget(); ?> PHP code with Line Number <!--?php $this--->beginWidget('CTextHighlighter',array('language'=>'PHP', 'showLineNumbers'=>'true')); ?> /** * SiteController is the default controller to handle user requests. */ class SiteController extends CController { /** * Index action is the default action in a controller. */ public function actionIndex() { $model=new DataModel(); if(!emptyempty($_POST[DataModel])) { $model->attributes=$_POST[DataModel]; if($model->validate()) $success=true; } $this->render('index', array( 'model' => $model, )); } } <!--?php $this--->endWidget(); ?>
通過指定language 的種類(不區分大小寫) 指明需顯示代碼的種類 。
本例下載:http://www.imobilebbs.com/download/yii/TextHighlighterDemo.zip