Yii Framework 开发教程(18) UI 组件 TextHighlighter示例

jerry Yii 2015年11月24日 收藏

CTextHighlighter用来格式化显示代码,目前支持显示的语言有ABAP, CPP, CSS, DIFF, DTD, HTML, JAVA, JAVASCRIPT, MYSQL, PERL, PHP, PYTHON, RUBY, SQL, XML,显示代码时也可以显示行号,通过showLineNumbers=TRUE打开行号显示。
显示PHP代码,一个带行号,一个不带行号:

  1.  
  2. PHP code
  3.  
  4. <!--?php $this--->beginWidget('CTextHighlighter',array('language'=>'PHP')); ?>
  5. // include Yii bootstrap file
  6. //require_once(dirname(__FILE__).'/../../framework/yii.php');
  7. $yii='C:/yiiframework/yii.php';
  8. // remove the following line when in production mode
  9. defined('YII_DEBUG') or define('YII_DEBUG',true);
  10.  
  11. $config=dirname(__FILE__).'/protected/config/main.php';
  12.  
  13. // remove the following line when in production mode
  14. // defined('YII_DEBUG') or define('YII_DEBUG',true);
  15.  
  16. require_once($yii);
  17. Yii::createWebApplication($config)->run();
  18. <!--?php $this--->endWidget(); ?>
  19.  
  20. PHP code with Line Number
  21.  
  22. <!--?php $this--->beginWidget('CTextHighlighter',array('language'=>'PHP',
  23. 'showLineNumbers'=>'true')); ?>
  24. /**
  25. * SiteController is the default controller to handle user requests.
  26. */
  27. class SiteController extends CController
  28. {
  29. /**
  30. * Index action is the default action in a controller.
  31. */
  32. public function actionIndex()
  33. {
  34.  
  35. $model=new DataModel();
  36.  
  37. if(!empty($_POST[DataModel]))
  38. {
  39. $model->attributes=$_POST[DataModel];
  40.  
  41. if($model->validate()) $success=true;
  42.  
  43. }
  44.  
  45. $this->render('index', array(
  46. 'model' => $model,
  47.  
  48. ));
  49. }
  50. }
  51. <!--?php $this--->endWidget(); ?>

通过指定language 的种类(不区分大小写) 指明需显示代码的种类。

201212125004

下载地址