Note: This article is marked deprecated. It means that it might contain old and/or outdated information that is no longer accurate or even relevant.


Syntax Highlighting for CakePHP, updated cakephp_geshi helper

I’ve updated my fork of Mark Story’s cakephp_geshi plugin. The two biggest changes are syncing to the upstream repository, as well as reintroducing the ability to use the geshi.php configuration file for backwards compatibility with existing installations. And of course the README has been updated significantly to communicate these changes.

So what’s this cakephp_geshi plugin anyway? Well, GeSHi is the Generic Syntax Highlighter, a PHP library for automatically performing syntax highlighting on code samples displayed on your websites, and cakephp_geshi is a plugin that makes GeSHi very easy to use in your CakePHP applications.

Programmers know that syntax highlighting is critical for quick understanding of code, and the same goes double when encountering new code samples online. Why not do your users a favor and implement some type of syntax highlighting on your own site?

Why a fork?

When I transitioned from the old version of to the new, current version I made it a requirement to maintain the old content, especially content that has significant historical interest. But just because content is obsolete doesn’t mean that it has to look bad. As such, I made an effort to find a syntax highlighter for the code samples present in Balthisar’s Guide to Mac OS X Printing. I considered several options, but when I stumbled upon Mark’s CakePHP-built solution, I stopped considering all other options because, well, this website is run on CakePHP. There was only one problem: it used a single configuration file that affected GeSHi’s behavior for the whole controller. I knew that I had to make a change.

Here’s an example of syntax highlighting a Unix bash shell script:

You see, my site uses a lot of static content. You know: hand-written HTML files that may be more than 10 years old. I used a couple of different controllers to manage all of these files, including the built-in Pages controller and a pretty neat multi pages controller. Being limited to a single GeSHi configuration meant limiting my syntax highlighting to a single language for much of my content. On a site such as, that’s just not feasible. You may have looked at the bash script sample above, but my interests are varied. The same syntax highlighter for bash won’t work for Objective-C.

And so, my fork dispenses with the need for a configuration file (although, the latest version re-introduces the option to use a file, if you still like to). Instead you can set the configuration directly from a view. In fact, your view can use GeSHi to highlight multiple code samples in different languages on the same page as long as you’re willing to part with the configuration file.

Additionally if you prefer to put view configuration in your controller, you can initialize the view settings in code right from the controller. Thanks to the original upstream author for making this suggestion!

Which should I use?

Mark Story is a prolific coder and a genuine CakePHP expert. If you’re always looking for cutting edge, you might want to stick with his version.

As of today, though, my version is in 100% in sync with his upstream version but also offers the benefits I described above. Check it out in the github repository.

comments powered by Disqus