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
- 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).
- Create it and upload the PHP file to your Smarty plugins folder.
- Ensure that GeSHi is installed somewhere on your site.
- 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!
thanks very much for this plugin.
i’ll use it.
Hi there,
Have you written any plugins for the PMWIKI cookbook receipes. There is a product called simple-groupware that uses SMARTY as its template engine but also uses PMWIKI ; I’ve already contacted the developers but they do not import any cookbook receipes from PMWIKI into SMARTY ; so I was wondering if you have done any and was wondering if you have a good example ,so I can then do them myself,
Kind regards
Lazarus
PS – I am quite a simple noddy user not having done any scripting of any kind find this stuff quite confusing hence if there are some basic steps to follow I am good at that.
I am trying to fetch database values from mysql which have code samples within the cells of one of the columns. When I fetch into smarty I use a foreach loop to iterate through all values. I can not seem to figure out how to output the value of the smarty variable within the literal tag as you have mentioned. How can I store the codes of my programs in a mysql database and then fetch the values of the codes and output them using literals to smarty. Thank you for the help.