This blog is about the PHP solutions I have to think of when I'm developing systems almost every single day...

Monday, February 11, 2008

Update on rendering element in CakePHP

I am not sure whether I made a mistake in the previous post or something has changed in cake. Anyhow, I wasn't able to do a simple $this->renderElement call to render my elements. After a bit of testing and trying it out some more, it seems you've got to actually echo out the result. So in the end it had to be like this:


<?php echo $this->element('topbanner');>


Notice also that I'm just saying just element rather than renderElement.

2 comments:

Gamma Sigma Phi said...

you have a sample on this? i don't see any sample in implementing elements.

sukran

Abdullah Zainul Abidin said...

You mean sample in actually writing an element? It's easy. What you do is write your element like a normal view but put it into the app/views/elements folder. So for example you might want to write a small element for a side menu which you use in many of your views, you might just write it in the file app/views/elements/sidemenu.ctp. Then inside your other views you do an:
echo $this->element('sidemenu');
You can even send variables to your elements. For example you might want to have a small welcome at the top of every view you have. So you might write app/views/elements/greeting.ctp with something like this:
<?php
echo "Hello ".$username;
?>
And then in your other views you can send in the $username variable by calling the element like this:
echo $this->element('greeting',array('username'=>'Brad Pitt'));

Hope that helps.

About Me

My Photo
Abdullah Zainul Abidin
I am what I am because of what I am...
View my complete profile