Note: This article has been submitted to the Bakery, but it’s been pending approval for a couple of weeks now and I got bored of waiting, so I’m just posting it here. I will update with the Bakery link if/when it goes live!
Despite having written several bits of potentially re-usable code for my Cake apps over the years, this is the one that’s really stuck out with potential to become popular and useful to the masses. It’s been in action for years on Simon’s FFL, and more recently on Covers.FM, but a recent question on Stack Overflow prompted me to polish it up, document it and release it. It also gave me a chance to try out GitHub, which seems to be what the cool kids are using to publish/share/collabrote on code these days.
In a nutshell, the Sidebar helper allows you to insert a sidebar with multiple content blocks into your layouts, similar to WordPress widgets.
So, without further ado, here’s the GitHub link:
http://github.com/ebotunes/cakephp/blob/a08a2011246a256fa1425bf07c98ff064436d527/helpers/sidebar.php
The code comments contain examples of usage to get you started. Technically speaking this doesn’t need to be used as a Side Bar – it ultimately depends on how you render the layout with CSS.
[I'll hopefully update this post with screenshots and sample CSS at some point when I've got more time. Just wanted to get the main code out there for now]
hello,
why this is better than using directly elements and inline code?
thanks, regards
danielz
Hi Daniel,
There are a few advantages to using this helper. Firstly, it allows you to give a consistent markup ‘wrapper’ to your sideboxes. Each time you use an element as a sidebox, it will be wrapped with
<div class="sidebox">...</div$gt;which allows you to style them consistently. If you were just to use elements normally you would need to include this wrapper div in every element file – nightmare if you decide to change the class name. Using the SideBar Helper your element files need only contain the contents of the box.
Also, and perhaps more importantly, ithe Helper is allowing you to inject elements into your layout file, from within the view file for each action. Typically your view file only affects the
$content_for_layoutportion of your layout, but with the Sidebar Helper you can control other parts of the layout (namely the$sidebar->getSidebarbit) from your view file.Hope that makes sense – let me know if you have any other questions – I realise it could do with some more example code and screenshots!!
Thanks,
Iain
I’m the one who posted the question on StackOverflow.com and I wanted to thank you and to say that i used your solution since then and never looked back. THANKS!