Whilst browsing the CakePHP API documentation this morning i stumbled upon the class Flay. According to the documentation it’s a textformatter like Textile.
This got me interested and i tried searching for some documentation about flay in cake. Unfortunately i couldn’t find anything on the net. So i dove into the code and tried some tags to see what it does.
To use flay you can either use the Texthelper
In your controller do:
var $helpers = array('Text');
And in your view do:
flay($text); ?>
You can allow html tags by using this code instead of the above (there are some issues with this tough, see bottom of article):
<?php echo $text->flay($text, true); ?>
You can also use the uses(’Flay’); in your controller/model
uses('flay');
class <ClassName> extends AppModel {...}
This way you get some extra features to use within the Flay class like:
colorMark($words, $string)
extractWords($string)
fragment($text, $length, $ellipsis= '...')
log($msg, $type=LOG_ERROR)
markedSnippets($words, $string, $max_snippets=5)
requestAction($url, $extra=array())
toClean($text)
toHtml($text=null, $bare=false, $allowHtml=false)
toParsedAndClean($text)
toString()
See more about the other features at docs.cakephp.nu
It has a few nice features to make your submitted text a little nicer.
First of it replaces the double newline with a paragraph, saving you the typing of
<p> and </p>
To force a paragraph you could also type
%PARAGRAPH%
For a linebreak type:
%LINEBREAK%
You can also make blockquotes:
%BLOCKQUOTE% ... %ENDBLOCKQUOTE%
Or center the text with
%CENTER% ... %ENDCENTER%
To format the text strong/bold or emphasis/italic use
*strong* or _emphasis_
It also does some basic url detection/formatting
links beginning with http:// are automatically formatted to a link, so no more
<a href=""></a>
To force a link type
[link]
where link is the actual url.
Images are also automatically detected so instead of
<img src="http://url.com/image.jpg"/>
type
http://url.com/image.jpg
Same goes with E-mail adresses.
These are the basic features of Flay and for some basic html formatting it’s really nice to use. But if you want to add a title to your link for example, or use alt text for your image you need to use the html tags.
And that’s where the problem is. Flay breaks html code, because it replaces the < and > tags. So a
<a href="cakephp.org" title="link to cakephp">CakePHP</a>
becomes
<a href="<a href="cakephp.org"">cakephp.org"</a> title="link to cakephp">CakePHP</a>
wich obviously is broken.
For simple formatting Flay does the job well and for most users of simple sites it’s sufficient. But if you want more semantic html (alt-tags and titles) you have to use one of the other formatting tools like Textile