Theming your CakePHP apps (V1.2)

All folders called ‘Themed’ are renamed to ‘Theme’ in CakePHP version 1.3 everything else in this post still holds true

There has been support for themes / skins for CakePHP since eons – in fact we had put in themes in Cheesecake Photoblog right in version 1.x, however this feature is not very well known. In CakePHP v1.2 theming has matured a bit and is now part of the core. Despite this there are continued request for information on how to use this functionality on the list and even googling for cakephp + theme does not really return anything definitive.

Once you have figured out how to use theming, adding new themes is errr… ummm… a piece of cake (sheesh talk about cliches!). The following steps will get you started with theming.

#1 In your app_controller add

  1. var $view = 'Theme';

#2 Next you have to tell your application the name of the theme you want to use – I do it in beforeFilter() , beforeRender() might be a good place as well with something like

  1. $this->theme = "mythemename";

in Cheesecake Photoblog the line reads

  1. $this->theme = $this->Session->read('cheeseCake.theme');

Thats it for the coding part, rest is just knowing where to put the views and other web resources for your theme.

The theme view class searches for views inside a folder called by your theme name inside the themed folder in view… Confused? Lets continue with the example of a the theme named “mythemename”

The views for this theme will reside in

  1. /app/views/themed/mythemename/

and will mimic the same structure as the main app – thus the default layout for this theme will be

  1. /app/views/themed/mythemename/layouts/default.ctp

A view for index action of photos controller would be

  1. /app/views/themed/mythemename/photos/index.ctp

A notable part of theming in CakePHP is that it has a fallback built in – if a particular view is not found within the themed folder then the view from the main app will be considered. Thus in the DRY spirit you need not replicate each and every view in your theme, the parent view will be used automagically.

If you have tried theming any application before you might be wondering what about the resources like CSS, Images and Javascript? Don’t despair – the theme specific webroot resources reside in a themed folder inside the webroot. So a css file called style.css for the above example would be in

  1. /app/webroot/themed/mythemename/css/

Another thing to note here is that the HTML and Javascript helpers are theme aware and will link to appropriate css, images, and js files (yes the fallback is built into these as well)

Lastly even the plugin views can be themed but I will leave that as an exercise to the reader to figure out…

About Tarique Sani

Dr. Tarique Sani is a pediatrician and forensic expert by education. He is a PHP programmer of 'wrote the book' caliber and has to his credit several very popular open source as well as commercial PHP projects. He leads a team of dynamic programmers at SANIsoft who have in-depth understanding of Web development tools and usability practices with strong developmental skills in PHP, MySQL/PostgreSQL, HTML, DHTML, Javascript, and Linux/Apache

19 Responses to Theming your CakePHP apps (V1.2)

  1. irfan ahmed March 24, 2008 at 6:57 pm #

    i am new to MVC, and find the cakephp easier than codeignitor,symfony or all other sibling frameworks

    I just bumped into the dynamic gradient fill of div instead of image backgrounds.

    http://www.designdetector.com/demos/css-gradients-demo-3.php

    themes thus created will have a web2.0+ look rather than mundane solid fill colored pages.

  2. Webmaster Mexico March 26, 2008 at 12:26 pm #

    I use theme for my app, it’s a cool. Thanks for the tip.

  3. Noah April 4, 2008 at 9:30 pm #

    Stupid question, but can you direct me to a resource on how to create a theme? What tokens do I need to use in the body of the html for content, navbar, etc? And when you say “Another thing to note here is that the HTML and Javascript helpers are theme aware and will link to appropriate css, images, and js files (yes the fallback is built into these as well)” how do I refer to my css stylesheet which is in webroot/themed/mytheme/css/style.css?

  4. Tarique Sani April 5, 2008 at 9:59 am #

    @Noah – $html->css(‘style’) will work, there is no more hidden magic.

  5. Jason Leveille September 26, 2008 at 8:27 pm #

    Thanks for the article. I’ve known that themes were available, however it wasn’t until 10 minutes ago that I implemented the native theme capabilities of Cake (and it only took about 10 minutes).

  6. John Arroyo July 15, 2009 at 12:45 pm #

    Good overview of theming, thanks!

    I started using themes in Cake 1.2 but am having trouble using layouts within these themes. Is it possible to use layouts and themes together or do you have to use only one at a time.

  7. Chris Ergatides May 23, 2011 at 4:54 pm #

    Themes have changed in CakePHP 2.0.0

    For further information, please check out http://blog.ergatides.com/web-design/themes-in-cakephp-2-0-0/

Trackbacks/Pingbacks

  1. PHPDeveloper.org - January 1, 2008

    Sanisoft Blog: Theming your CakePHP apps (V1.2)…

    The Sanisoft blog has a quick post covering the theming of ……

  2. developercast.com » Sanisoft Blog: Theming your CakePHP apps (V1.2) - January 1, 2008

    [...] Sanisoft blog has a quick post covering the theming of a CakePHP application (via the functionality in the core of the framework). [...]

  3. links for 2008-01-01 « Richard@Home - January 1, 2008

    [...] SANIsoft – PHP for E Biz» Blog Archive » Theming your CakePHP apps (V1.2) A simple howto use themes in CakePHP (tags: cakephp themes tutorial) [...]

  4. Web Developer Blog » Blog Archive » CakePHP Theme View - January 3, 2008

    [...] http://www.sanisoft.com/blog/2007/12/29/theming-your-cakephp-apps-v12/ [...]

  5. CakePHP Weekly Summaries Issue #1 | Cakephp.nu blog - January 5, 2008

    [...] Max asked if anyone had a component/helper for Themes management for an app ?. Tarique shamelessly pointed to his own blog, where there is quick guide on how to make cakephp use ThemeView. [...]

  6. Theming With CakePHP :: PseudoCoder.com - January 7, 2008

    [...] There have been a couple posts lately on the CakePHP Google Groups asking how themes work. I responded to one with the some quick basic steps. There is also a post by Tarique Sani that give a pretty good starting point. [...]

  7. Blog Dot Php With cakePHP » Полезные ссылки по cakePHP - January 26, 2008

    [...] Использованием нескольких тем оформления (view) [...]

  8. PHP-ist: All about learning PHP, frameworks, tips and tricks » CakePHP: How to Add Theme Support? - February 6, 2008

    [...] v1.2 (beta) we can easily add theme with its ThemeView class. I learn this from Sanisoft blog’s quick post which covering the theming of a CakePHP [...]

  9. CakePHP Tutorials :: PseudoCoder.com - February 11, 2008

    [...] – Theming your CakePHP apps (V1.2) http://www.pseudocoder.com – Theming With [...]

  10. CakePHP theme made easy | rodoTelmi - October 15, 2008

    [...] theme tutorial Theming your CakePHP apps (V1.2), Theming With CakePHP Bookmark It Hide Sites [...]

  11. Template par défaut pour CakePHP pour votre site en CakePHP « Wareteam - November 3, 2008

    [...] http://www.pseudocoder.com/archives/2008/01/07/theming-with-cakephp/ CakePHP themes made easy Theming your CakePHP apps (V1.2) [...]

Leave a Reply