CakePHP Geshi helper
26/04/2008
I have written a small Geshi helper for CakePHP, with Geshi you can display formatted code with colours. Just drop Geshi in your vendors directory and create a a helper to your taste.. personally I use a style helper which contains different methods to help format an article.
Now just create <pre class="php"> code goes here </pre> in your article and your done. Replace php with the language of your choice.
- <?php
- class StyleHelper extends AppHelper {
- /**
- * This method Formats pieces of code with code coloring
- *
- * @access public
- * @param string $content
- * @return string
- */
- public function formatCode($content) {
- App::import('Vendor','Geshi');
- $this->geshi = new GeSHi('', '');
- $this->geshi->set_header_type(GESHI_HEADER_DIV);
- // Turn on fancy lines
- $this->geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS, 2);
- // Enable css class names
- $this->geshi->enable_classes();
- <pre class=\"([^\"]*)\">(.*?)</pre>
- }
- /**
- * Method to return the formatted code by Geshi
- *
- * @access private
- * @param string $matches
- * @return string
- */
- private function replaceCallback($matches) {
- $this->geshi->set_language($matches[1]);
- return $this->geshi->parse_code();
- }
- }
I have this function in a helper called style, but you can name it anything you like. To use it you can just load enter your content in the view and the formatted code will be returned.
- <?php
- ?>