Yii 1.1: syntaxhighlighter

Wrapper for the Alex Gorbatchev SyntaxHighlighter
8 followers

SyntaxHighlighter component for Yii framework.

This application component wraps SyntaxHighlighter by Alex Gorbatchev

Install

Unpack extension into a directory of your choice, the extensions directory would be a good choice.

Add to config/main.php:

'components' => array(
            'syntaxhighlighter' => array(
                'class' => 'ext.JMSyntaxHighlighter.JMSyntaxHighlighter',
            ),
            ...
    ),

If you want a theme other than the default:

'components' => array(
            'syntaxhighlighter' => array(
                'class' => 'ext.JMSyntaxHighlighter.JMSyntaxHighlighter',
                'theme' => 'Django',
            ),
            ...
    ),

You can choose from the following themes:

Default (the default if none provided)
Django
Eclipse
Emacs
FadeToGrey
MDUltra
Midnight
RDark

Usage

Insert this code where you want the syntaxhighlighter script to be:

<?php Yii::app()->syntaxhighlighter->addHighlighter(); ?>

This could be in your main layout, but it's up to you.

The script will highlight any pre tag having the "brush" class:

<pre class="brush : php">
function Hello($world) {
    echo 'Hello ' . $world;
}
</pre>

Available brushes are:

    _Brush_                 :   _Language_
    applescript             :   AppleScript
    actionscript3 as3       :   AS3
    bash shell              :   Bash
    coldfusion cf           :   ColdFusion
    cpp c                   :   Cpp
    c# c-sharp csharp       :   CSharp
    css                     :   Css
    delphi pascal           :   Delphi
    diff patch pas          :   Diff
    erl erlang              :   Erlang
    groovy                  :   Groovy
    java                    :   Java
    jfx javafx              :   JavaFX
    js jscript javascript   :   JScript
    perl pl                 :   Perl
    php                     :   Php
    text plain              :   Plain
    py python               :   Python
    ruby rails ror rb       :   Ruby
    sass scss               :   Sass
    scala                   :   Scala
    sql                     :   Sql
    vb vbnet                :   Vb
    xml xhtml xslt html     :   Xml

For example, to highlight a bash script, you can use either bash or shell:

<pre class="brush : shell">...</pre>

This component makes use of the SyntaxHighlighter autoload feature, so that only the brushes used are actually loaded into the page.

Tricks

TinyMCE is great, but it does not want to leave the <pre> tags alone! Don't worry, there's a solution to this problem: Grab and install the Wysiwyg Pre Element Fix and enjoy! :) Just in case: the Pre Element Fix has been attached to this page as well.

Resources

Total 3 comments

#18289 report it
Gregurco at 2014/10/11 06:24pm
Class container

The code is wrapped in class container, that is used in bootstrap. And if you check it in firefox, you will notice that the line of code is not on the same level with the number of this line. Because in bootstrap class container has next rule: content: " "

#16646 report it
Lexne at 2014/03/14 10:36am
Works perfectly

Works perfectly for me. Up and running in less then 5 minutes.

However, if I would not like to show the lines. Can I remove those with some easy syntax?

function Hello($world) { echo 'Hello ' . $world; }

#9665 report it
nirmalroka at 2012/09/02 08:54am
not working exactly

Hi I had used this but not working exactly. I configured as guide above.

my code here

I used the same in my text area not used any editor.

In my view I get missed first part of <?php and other paramerter. When i use <?php Yii::app()->syntaxhighlighter->addHighlighter(); ?>

in view where i display content than it show different al over the code

Leave a comment

Please to leave your comment.

Create extension