a tinymce element for cakePHP
To demonstrate the power of Elements in cakePHP, I’m going to use as an example TinyMCE. TinyMCE is basically a LGPLed web based Javascript HTML WYSIWYG editor. It has many features but it’s not the aim of this post to introduce it, rather to demonstrate how we can use it as an element. TinyMCE can be used on textareas ( and probably some other tags ) .
So we have this scenario: you’re building a ( yet another ) CMS. In the add, edit forms you decide to give the user the possibility to create/edit the page visually. So you probably have something that looks like this:
-
-
// cute 1.2 code
-
</form>
-
Now as a smart guy you create an element, you name tiny_mce.ctp and you put this code in it:
-
-
-
<script language="javascript" type="text/javascript">
-
tinyMCE.init({
-
theme : "advanced",
-
mode : "textareas"
-
});
-
</script>
-
-
You call renderElement before the form and you’re all set.
So what’s the issue ? Well first of all, I forgot to mention that the description field is a textarea and we don’t want it to use tinyMCE. You can give ‘body’ an id ( in fact the form helper does this ) and hard code it in the element; but then, elements are meant to be reusable. Here comes the time to utilize the power and flexibility they offer: Elements accept arguments. So to make a reusable and flexible element we’re going to use the element as a black box and tell it what fields we want to edit visually.
-
<script language="javascript" type="text/javascript">
-
tinyMCE.init({
-
theme : "advanced",
-
<?php
-
foreach($field as $f):
-
$fs[] = "data[$model][$f]";
-
endforeach;
-
?>
-
elements :"<?php echo implode(",",$fs)?>",
-
<?php else: ?>
-
-
elements : "data[<?php echo $model; ?>][<?php echo $field; ?>]",
-
<?php endif; ?>
-
});
-
</script>
-
And here is how we use it:
If we later decide we want to edit the description visually too we do this:
So that’s it, the second param of renderElement is an array, the keys are the variables names and the values are the values of these variables, cake manages to get them accessible within the element as simple variables. How neat !
Keep in mind this is just an example. if you want to improve it, first, remove script and use $javascript->block, second, use the second param of $javascript->link so that it’s included in the head ( of course you need to have $scripts_for_layout somewhere in the head of the layout) you can also cache that block in an external file if you choose to.
TinyMCE Element for CakePHP…
Here is a quick and easy way to integrate a WYSWYG editor into your CakePHP applications. This article is very straight forward and will get you using TinyMCE in no time at all!
To demonstrate the power of Elements in cakePHP, I’m going to use as exa…
Hi there,
you’re not planning to write a complete tutorial by any chance? I mean where you describe that code optimization and also how to ajax the text into a database, having an edit mode where text can be edited and a mode where the result is shown, how you switch between those modes etc.
Would be helpful to those of us who still can’t see the big picture…
Thanks and best regards,
Mickster
I forgot to mention - I have read the tutorials in the bakery, but still the big picture is missing…
Hello.
Thanks for this Post.
I tried to implement this but i have to add:
mode : “exact”,
In tinyMCE.init to make it work.
But thanks any way.
Carlos Timóteo
Portugal