February 29th, 2008
PHP / mySQL
Jump to comments
I couldn't find any helper for CakePHP to implement GeSHi, so I had to write my own. Bellow are the results of this helper in action.
To be able to use this helper you need to download GeSHi and put it in /app/vendors/geshi directory. You only need geshi.php and the geshi folder.
/app/views/helpers/formatting.php
Load the helper into your cotroller var $helpers=Array('Formatting');. To tell the helper what to highlight, the text from your view must be between the <code></code> tags. You can specify the language you wish to highlight like this: <code language="php"></code>. If no language is defined, the default language is set to php.
For example:
The results are similar to this page and should work without any problems. This helper is highlighting only php code. It can be extended for any type of language.
Enjoy!
To be able to use this helper you need to download GeSHi and put it in /app/vendors/geshi directory. You only need geshi.php and the geshi folder.
/app/views/helpers/formatting.php
<?php App::import('Vendor','geshi'.DS.'geshi'); class FormattingHelper extends Helper{ function parse_code($code){ if($code[1]=="") $language="php"; $geshi->set_header_type(GESHI_HEADER_PRE); $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS); $geshi->start_line_numbers_at(1); return $geshi->parse_code(); } function geshi($text){ return $geshi; } } ?>
Load the helper into your cotroller var $helpers=Array('Formatting');. To tell the helper what to highlight, the text from your view must be between the <code></code> tags. You can specify the language you wish to highlight like this: <code language="php"></code>. If no language is defined, the default language is set to php.
For example:
<?php $text='<code language="php">'; $text.='echo\'Hello world! \';'; $text.='for($i=0;$i<10;$i++){ echo $i; }'; $text.='</code>'; ?>
The results are similar to this page and should work without any problems. This helper is highlighting only php code. It can be extended for any type of language.
Enjoy!
March 3rd, 2008 at 09:23:52
Thank you, it works great.
Im only trying to get it work with TinyMCE but it seems that TinyMCE converts everything to normal html even if i change the html myself. I tried editing the .js file but it got way to much line and im not an expert, maybe you know a solution?
March 3rd, 2008 at 09:42:33
To get it work with TinyMCE change the <code> tag to <pre> in /app/views/helpers/formatting.php
Thats it.
March 3rd, 2008 at 11:53:50
@Ryatzu: Instead of putting in your <code> html tags, use < for < and > for >.
March 3rd, 2008 at 18:55:33
thanks Gurde, I can use this :)
March 20th, 2008 at 15:38:42
This works very well, and is definitely going to be useful in my projects. Many thanks!
March 29th, 2008 at 07:24:44
@Scott Sanders: I am pleased to know it's useful for somebody.