Dynamic elements in rich text editors
Using rich text editors like TinyMCE or FCKEditor eases life to the lweb editors. At the same time using these editors can represent a problem if content is meant to consist dynamic elements that are managed somewhere else.
Some examples for dynamic elements could be text or image assets (that are managed in assets section of their favourite CMS) or business card of employee that you want to make possible for editor to insert into content with one click and would change automatically if employee changes its telephone number.
My idea is to insert dynamic element as command directly into HTML code. This command would be processed just before the page is requested by client. I decided to use HTML comments to specify the command itself so the command does not mess with normal code. So my command can look something like this:
<!--parse:date format=”Y-m-d”-->[dynamic date]<!--/parse:date-->
[dynamic date] should be replaced with current time in format specified by format attribute. SimpleParser is simple class that is responsible to parse dynamic elements. You can download SimpleParser class in goodies section. SimpleParser will call specified callback function with specified parameters every time it encounters parse command in HTML comment and replace content between command start and command end with return value of callback function.
$simple_parser = &new SimpleParser('execute_plugin');<br />$content = $simple_parser->parse($content); <br /><br /><br />function execute_plugin($command_name, $params, $value) {<br /> switch ($command_name) {<br /> case 'date' : <br /> $result = " " . date('d.m.Y H:i:s') . " ";<br /> break;<br /> default:<br /> $result = "$command_name is not defined.":<br /> }<br /> return ($result); <br />} Check the dynamic elements demo (will open in new window), created using TinyMCE rich text editor. Please note that versions of TinyMCE editor before and including 2.0RC4 have some bugs with inserting comments on IE. Many thanks to spocke for fixing them.
I would like to hear from you experiences with dynamic element as well as comments and suggestions regarding these method.
I am Bojan Mihelac and this blog is dedicated to share code, thoughts, tools and advices I came up with while working in
2 comments
Speak your mind: