GeSHi is a free, PHP syntax highlighter – invaluable if you need to display code in your webpages. I use it all over the place, since it really goes a long way to making your code more readable.

Anyway, Smarty has many plugins available but surprisingly, there wasn’t one for GeSHi integration. So I wrote one. Here it is. Go crazy.

Installation

  1. Click the “Fork on on Github” link to go to the source. It’s a single file called block.geshi.php (the filename is important, don’t change it).
  2. Create it and upload the PHP file to your Smarty plugins folder.
  3. Ensure that GeSHi is installed somewhere on your site.
  4. Edit the block.geshi.php file and update the require_once() line, so that it points to your geshi folder.

And that’s it! You’re done installing it.

Usage

Using it is really simple. It’s implemented as a “block” function – meaning you just wrap your code in the geshi tags and it automatically converts it using GeSHi. Try embedding this in your smarty template:

1
2
3
4
5
6
7
8
9
{geshi lang="php"}
{literal}
function chicken()
{
  echo "chicken!";
  return true;
}
{/literal}
{/geshi}

Note the extra {literal} and {/literal} tags. The default tag delimiter for Smarty is the curly brace { and }. Since a lot programming languages use these characters, you’ll need to tell the Smarty engine not to try and convert the code first. Hence the {literal} tags. If however, you specify your own your right and left Smarty delimiters, the {literal} tags may not be necessary.

The lang parameter is required, and lets GeSHi know which language the code is in.

There are just two additional parameters:

1
2
3
4
5
6
7
{geshi lang="php" show_line_numbers=true start_line_numbers_at=X}
function chicken()
{
  echo "chicken!";
  return true;
}
{/geshi}

I think they’re self-explanatory.

Anyway, hope this comes in handy!