GeSHi - Generic Syntax Highlighter Logo

How To Use Joomla's GeSHi Plugin?

Boy, isn't it a nightmare when you need to do something simple and there's no documentation out there? That's, unfortunately, the case with Joomla's core GeSHi plug-in. GeSHi is a nice, but simple syntax highlighter. So if you have the need to show your visitors a snippet of code, GeSHi makes it far more readable. Here's an example without GeSHi:

<?php
// Here are a few lines of Joomla code
defined('_JEXEC') or die;
function getBaseURI() {
  return JURI::base();
}
?>

 

Here's an example using GeSHi:

<?php
// Here are a few lines of Joomla code
defined('_JEXEC') or die;
function getBaseURI() {
  return JURI::base();
}
?>

How to use GeSHi in Joomla!

A GeSHi plug-in comes neatly packaged with Joomla! right out of the box. As I said at the top, there's no good documentation out there. So, I cracked open the source code to figure it out. To make life a little easier for you, here's how to use it:

Enable the GeSHi Plug-in

Out of the box, Joomla! disables the GeSHi plugin. Just like with any Joomla! plug-in, you've got to go to the Plug-in Manager, find it and Enable it for it to work. It's a Content plug-in and you can filter on GeSHi.

Mark Your Content

You'll need to tell GeSHi which code to highlight. Do that using the html <pre> tag, with a little twist. First, tell the GeSHi plugin which language you're using, with an xml:lang attribute. If you wish, you can follow that with a lines='true' attribute, which adds simple line numbers. That's all the core plugin supports. I almost forgot, the core plugin doesn't support all the languages, only the following:

 
  • css
  • php
  • xml
  • sql
  • mysql
  • php
  • php-brief
  • ini
  • diff
  • javascript
 

Examples

Here are two examples:

  • <pre xml:php> - will highlight php formatted code
  • <pre xml:css lines="true"> - will highlight css formatted code, with line numbers.

Oh, one more thing. Since the <pre> tag shows contents as is, you may want to skip the new line after the <pre> tag and before the </pre> tag, like this:

<pre xml:php lines="true"><?php
// Here are a few lines of Joomla code
defined('_JEXEC') or die;
function getBaseURI() {
  return JURI::base();
}
?></pre>