GeSHi - Generic Syntax Highlighter

  Previous article Next article  

Add the Generic Syntax Highlighter (GeSHi) to the frontend of your website.
The idea for this article originates from the former CMS Made Simple Wiki and the original author was tsw.

  How to use

1. Create a file named block.cms_geshi.php with the following content:

 * GeSHi - Generic Syntax Highlighter
 * -------------------------------------------------------------------
 * File:     block.cms_geshi.php
 * Type:     block
 * Purpose:  highlight a block of text using GeSHi
 * -------------------------------------------------------------------

function smarty_block_cms_geshi($params, $content, &$smarty)
  if (isset($content))
    include_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'cms_geshi' . DIRECTORY_SEPARATOR . 'geshi.php');

    $lang = isset($params['lang']) ? $params['lang'] : 'php';
    $styles = isset($params['styles']) ? $params['styles'] : '';

    $geshi = new GeSHi($content, $lang,  '', true);

    $uid = get_userid(FALSE);
    if ( !empty ($uid) ) echo $geshi->error();

    // Show title in header
      $title = isset($params['title']) ? $params['title'] : '';
      if (!empty($title)) { $geshi->set_header_content($title); }

    // Show linenumbers

    // Show CSS classes

    // Show text indent, etc.

    // Show external links to PHP documentation

    // Set target to external links
      $geshi->set_link_target('_blank', $styles);

    echo $geshi->parse_code();

2. Download the GeSHi files and unzip the archive.

3. Create a new folder in the core plugins directory named cms_geshi and upload the GeSHi folders and files in it.

4. Move the created block.cms_geshi.php file to the plugins folder.


5. Copy the GeSHi stylesheet content into a new CMSMS stylesheet and attach it to the template.

Refer to GeSHi documentation on GeSHi's capabilities GeSHi doc.

Now you can have highlighted code by calling this block from your page with:

    echo 'foo bar';
    if ($test == true) { echo 1; }
{cms_geshi lang='php' title='foo'}{literal}
    if ($test == true) { echo 1; }

Note that you need {literal}{/literal} tags around your code when braces are used inside.

  Working example

Just look above, I use it here at the website! :)

Buy Me A Coffee

  Comment Form


Click here to open the form



Buy Me A Coffee

CMS Made Simple - Tutorials, Tips and Tricks - CMSMS

GeSHi - Generic Syntax Highlighter

  Article optimized for CMSMS 2.x

  Last tested in: CMSMS 2.2.19
  Last updated: 10-02-2019
  Comments: 7

Buy Me A Coffee


Ads help me to help you! Thanks!

Ads help me to help you! Buy products from these advertisers!