If you are posting code, be it company's internal wiki or a public developer's blog, it is always better to have the syntax highlighted, so ones who read the code will feel comfortable.
Yii has Pear Text_Highlighter code-highlighting class bundled. It is used to highlight Yii definitive guide examples and we can use it to do the same for our application.
In this recipe, we will create a simple application that will allow adding, editing, and viewing code snippets.
Getting ready
- Create a new application by using yiic webapp as described in the official guide at the following URL: http://www.yiiframework.com/doc/guide/en/quickstart.first-app
- Set up a database connection and create a table named snippet as follows:
CREATE TABLE `snippet` ( `id` int(11) unsigned NOT NULL auto_increment, `title` varchar(255) NOT NULL, `code` text NOT NULL, `html` text NOT NULL, `language` varchar(20) NOT NULL, PRIMARY KEY (`id`) );
How to do it...
- First, we will tweak the protected/models/Snippet.php model code. Change rules method to the following:
- Add methods to the same Snippet model:
- The model is ready. Now we will create a controller. Therefore, create protected/controllers/SnippetController.php as follows:
- Now views; create protected/views/snippet/index.php as follows:
- title), array('view', 'id' => $model->id) ) ?>
- Create protected/views/snippet/view.php as follows:
- Create protected/views/snippet/add.php as follows:
- Create protected/views/snippet/edit.php as follows:
- Create protected/views/snippet/_form.php as follows:
- getSupportedLanguages()) ?>
- That is it. Now run the snippet controller and try creating code snippets, as shown in the following screenshot:
- When it is viewed, it will look similar to the following:
public function rules() { return array( array('title, code, language', 'required'), array('title', 'length', 'max' => 255), array('language', 'length', 'max' => 20), ); }
protected function afterValidate() { $highlighter = new CTextHighlighter(); $highlighter->language = $this->language; $this->html = $highlighter->highlight($this->code); return parent::afterValidate(); } public function getSupportedLanguages() { return array( 'php' => 'PHP', 'css' => 'CSS', 'html' => 'HTML', 'javascript' => 'JavaScript', ); }
order = 'id DESC'; $models = Snippet::model()->findAll(); $this->render('index', array( 'models' => $models, )); } public function actionView($id) { $model = Snippet::model()->findByPk($id); if (!$model) throw new CException(404); $this->render('view', array( 'model' => $model, )); } public function actionAdd() { $model = new Snippet(); $data = Yii::app()->request->getPost('Snippet'); if ($data) { $model->setAttributes($data); if ($model->save()) $this->redirect(array('view', 'id' => $model->id)); } $this->render('add', array( 'model' => $model, )); } public function actionEdit($id) { $model = Snippet::model()->findByPk($id); if (!$model) throw new CHttpException(404); $data = Yii::app()->request->getPost('Snippet'); if ($data) { $model->setAttributes($data); if ($model->save()) $this->redirect(array('view', 'id' => $model->id)); } $this->render('edit', array( 'model' => $model, )); } }
Snippets
→ title) ?>
$model->id)) ?>html ?>
→ Add snippet
renderPartial('_form', array('model' => $model))?>
→ Edit snippet
renderPartial('_form', array('model' => $model))?>
How it works...
The snippet model's function is used to store the code and snippet title. Additionally, we have added the html and language fields. The first one (html) is used to store HTML representing the highlighted code and the language field is used for the snippet language (PHP, HTML, CSS, JavaScript, and so on). We need to store these, as we need them when we edit the the snippet. As we remove the safe rule from the Snippet model, we make title, code, and language as the required fields. There is no rule for html which means that it cannot be set through the form directly. The afterValidate method—as its name states—is executed after the validation gives us no errors. In this method, we transform the code which is stored in the code field to HTML representing the highlighted code in the html field by using the Yii's CTextHighlighter class and passing the language value to it. getSupportedLanguages returns languages we want to support in the value-label array. We use this method in the snippet form.There's more...
In order to learn more about code highlighting, you can use the following resources:Yii code highlighter
- http://www.yiiframework.com/doc/api/CTextHighlighter
- http://pear.php.net/package/Text_Highlighter/
More code highlighters
If Text_Highlighter bundled with Yii does not fit your needs, then there are many code highlighters available on the Internet. A few good examples are:
- http://qbnz.com/highlighter/
- http://softwaremaniacs.org/soft/highlight/en/
I like to use Codelobster for Yii.
ReplyDeleteAnkara
ReplyDeleteAntalya
istanbul
Ordu
izmir
E357
görüntülüshow
ReplyDeleteücretli show
A1O
Adıyaman Lojistik
ReplyDeleteTrabzon Lojistik
Muğla Lojistik
Bayburt Lojistik
Bayburt Lojistik
OM0Vİ
rize evden eve nakliyat
ReplyDeletemuğla evden eve nakliyat
kırıkkale evden eve nakliyat
mardin evden eve nakliyat
istanbul evden eve nakliyat
31L
4EE55
ReplyDeleteErzurum Evden Eve Nakliyat
Kırıkkale Evden Eve Nakliyat
Hakkari Evden Eve Nakliyat
Nevşehir Evden Eve Nakliyat
Bayburt Evden Eve Nakliyat
009CD
ReplyDeleteEryaman Alkollü Mekanlar
Silivri Cam Balkon
Osmaniye Evden Eve Nakliyat
Batman Evden Eve Nakliyat
Van Evden Eve Nakliyat
12DC9
ReplyDeleteBolu Evden Eve Nakliyat
Çanakkale Evden Eve Nakliyat
Samsun Şehirler Arası Nakliyat
Kırıkkale Şehir İçi Nakliyat
Sivas Şehir İçi Nakliyat
Çerkezköy Sineklik
Çorum Şehir İçi Nakliyat
Diyarbakır Evden Eve Nakliyat
Keçiören Fayans Ustası
8E410
ReplyDeleteTrabzon Parça Eşya Taşıma
Zonguldak Lojistik
Sivas Evden Eve Nakliyat
Elazığ Lojistik
Denizli Parça Eşya Taşıma
04586
ReplyDeleteKırşehir Lojistik
Tunceli Şehirler Arası Nakliyat
Iğdır Lojistik
Elazığ Evden Eve Nakliyat
Etimesgut Boya Ustası
Mamak Fayans Ustası
Bitfinex Güvenilir mi
Siirt Evden Eve Nakliyat
Düzce Parça Eşya Taşıma
F08AD
ReplyDeleteAğrı Parça Eşya Taşıma
Osmaniye Evden Eve Nakliyat
Silivri Cam Balkon
Edirne Lojistik
Etlik Boya Ustası
Edirne Şehirler Arası Nakliyat
Çorum Lojistik
Iğdır Evden Eve Nakliyat
Çanakkale Parça Eşya Taşıma
EBF37
ReplyDeleteÜnye Oto Elektrik
Tunceli Parça Eşya Taşıma
Burdur Lojistik
Bolu Şehirler Arası Nakliyat
Kütahya Şehirler Arası Nakliyat
Tokat Şehir İçi Nakliyat
Altındağ Fayans Ustası
Maraş Şehirler Arası Nakliyat
Aksaray Şehirler Arası Nakliyat
E25FE
ReplyDeletePursaklar Parke Ustası
Bursa Lojistik
Bolu Evden Eve Nakliyat
Bitlis Şehirler Arası Nakliyat
Coinex Güvenilir mi
Afyon Parça Eşya Taşıma
Cointiger Güvenilir mi
Yenimahalle Boya Ustası
Kırşehir Evden Eve Nakliyat
B5B1F
ReplyDeletesteroids for sale
Çerkezköy Parke Ustası
Eskişehir Evden Eve Nakliyat
Kırklareli Evden Eve Nakliyat
primobolan for sale
order pharmacy steroids
sarms
Çerkezköy Fayans Ustası
buy winstrol stanozolol
9379E
ReplyDeleteŞırnak Evden Eve Nakliyat
Urfa Evden Eve Nakliyat
buy turinabol
buy deca durabolin
sarms
Siirt Evden Eve Nakliyat
Burdur Evden Eve Nakliyat
testosterone propionat for sale
Silivri Boya Ustası
821ED
ReplyDeletesohbet sitesi
tokat mobil sohbet et
çankırı canlı görüntülü sohbet
kırıkkale nanytoo sohbet
van canlı görüntülü sohbet siteleri
en iyi ücretsiz sohbet uygulamaları
Antalya Canlı Sohbet Odaları
en iyi ücretsiz görüntülü sohbet siteleri
random görüntülü sohbet
E2879
ReplyDeleteTiktok Takipçi Satın Al
Periscope Takipçi Satın Al
Bitcoin Madenciliği Nasıl Yapılır
Görüntülü Sohbet
Soundcloud Beğeni Satın Al
Nexa Coin Hangi Borsada
Bitcoin Nasıl Oynanır
Binance Borsası Güvenilir mi
Bitcoin Kazanma
06316
ReplyDeleteBinance Para Kazanma
Trovo Takipçi Satın Al
Youtube İzlenme Hilesi
Parasız Görüntülü Sohbet
Chat Gpt Coin Hangi Borsada
Qlc Coin Hangi Borsada
Keep Coin Hangi Borsada
Azero Coin Hangi Borsada
Facebook Takipçi Satın Al
C7B57
ReplyDeleteuniswap
uwu lend
phantom
solflare
avax
raydium
ledger desktop
aave
chainlist
BB951
ReplyDeletepoocoin
eigenlayer
pancakeswap
dao maker
yearn finance
phantom wallet
spookyswap
quickswap
thorchain