3 Hidden CakePHP Functions You Can’t Live Without, Flay
CakePHP’s fattie /cake/lib folder holds many hidden treasures, that although documented in Cake’s API, lacks examples of use and simple noobie terms like “howto”. I’m here to help, so settle down.
Note: In my examples I use each of the functions in a view file (/app/views/), rather than a controller (/app/controllers/) file. I suppose either one would work, though for these functions, it would probably make more sense to use them in a view.
Including the Flay class
In order to use the any one of the following functions in your views, you first need to make sure you include the Flay class. To do so, place the following at the top of the view you wish to use:
<?php
uses('Flay');
$flay = new Flay();
?>
1. fragment
Return a fragment of a text, up to $length characters long, with an ellipsis after it.
Code:
$text = "<p>This is some example text.</p>";
$text = $flay->fragment($text,7);
echo $text;
Html Output:
<p>This is...
The text is fragmented to closely match a character count of 7 without breaking up any words.
2. toHtml
Returns given text translated to HTML using the Flay syntax.
Code:
$text = "<p>This is some example text.</p>";
$text = $flay->toHtml($text,null,true);
echo $text;
Html Output:
<p><p>This is some example text.</p></p>
As you can see, the entire text field is surrounded in a p tag, and the HTML in the $text variable is used as actual HTML.
Code:
$text = "<p>This is some example text.</p>";
$text = $flay->toHtml($text,null,false);
echo $text;
Html Output:
<p><p>This is some example text.</p></p>
The entire text field is surrounded in a p tag, but this time the HTML in the $text variable is converted to HTML entities.
3. colorMark
Returns string with EM elements with color classes added.
Code:
$text = "<p>This is some example text.</p>";
$text = $flay->colorMark(array('some','text'), $text);
echo $text;
Html Output:
<p>This is <em class="yl">some</em> example <em class="gr">text</em>.</p>
According to the above, all words equaling some will have an EM element with the class “yl”. Where as all words equaling text will have an EM element with the class “gr”.
Let me know if I’ve missed anything.

These are very useful, thanks!
What is the meaning of the ‘yl’ and ‘gr’ class names? Can they be changed to something a bit more specific?
You’re very welcome Alex!
The
ylandgrclass names stand for “yellow” and “green”, respectively. Of course they can represent whatever color you like via CSS.It doesn’t look like they can be changed without directly hacking the function.
[...] 3 Hidden CakePHP Functions You Can’t Live Without, Flay [...]
Interesting. When I get time I just may go in and see about hacking the function to provide a more semantic set of class names. Of course, that’ll happen after I know what I’m doing with Cake. :)
Thanks a million, I had spent hours searching the net for examples of how to use the built in libraries, I’m relativity new to CakePHP. Any this was helpful and I never realized or took the time to pluck apart the Flay object. Well done.
Very nice things to get know about.
Thanks for the share!