Twilight SEO

Standing at the border line. Beautiful color spectrum of Search Engine Optimization. White, Gray, Black Hat SEO learning by doing.

CakePHP: How to Add Theme Support?

Posted in cakephp by ruru on the February 7th, 2008

Now with CakePHP v1.2 (beta) we can easily add theme with its ThemeView class.
This guide about how to add theme into fresh CakePHP installation will be explained in very basic step-by-step . I learn it frrom here.
Updated: sample code provided

Download code example, extract inside CakePHP root.

First!
Create a file called app_controller.php inside cake\app\controllers.
Create CakePHP AppController
Edit the file and just with two lines of code, add theme support on your application:

PHP:
  1. class AppController extends Controller {
  2.     var $view = 'Theme';
  3.     var $theme = 'default';
  4. }

Second!
You need to put all your theme collections inside a folder called themed instead views. This folder doesn't exists yet, so go to cake\app\views and create new folder themed inside it.
Create themed folder inside view

Third!
Create another folder called default inside cake\app\views\themed, and place all your template files there.
This theme name is taken from

PHP:
  1. var $theme = 'default';

We can just create another folder and activate it very easily thanks to ThemeView class.

That's it now you have an application that support theme. Easy?! Yea, that was sooo Cake :)

Tags: php4, cakephp, code example

5 Responses to 'CakePHP: How to Add Theme Support?'

Subscribe to comments with RSS or TrackBack to 'CakePHP: How to Add Theme Support?'.

  1. majic3 said,

    on March 30th, 2008 at 2:17 am

    folder called themed instead views

    instead should read inside?

    Themes are great I had a play with them a while back, found it perfect for making admin interface entirely separate from the public site. Had some issues when using it in conjunction with assetmapper, but since cake now has settings for asset compression in core.php I am revisiting this

  2. PHP-ist said,

    on March 30th, 2008 at 8:22 am

    …put inside folder “views/themed/”, instead “views/”.

    Thanks for your correction :)

  3. MoreToKnow said,

    on February 15th, 2009 at 10:30 pm

    Interesting feature! I’m left with at least two key questions right off the top of my head, though:
    (1) What about layouts?
    (2) What about css?
    Both of those assets determine the look of a Cake app, but they don’t fall under the /views/ folder. So, how do they play into this Theme support?
    Thanks

  4. Dan said,

    on March 24th, 2009 at 8:19 pm

    Hi,
    Same question - this is nice to handle layouts - but how would you handle
    e.g. allowing the user to pic their CSS scheme for the site?

  5. ruru said,

    on March 24th, 2009 at 9:11 pm

    Layouts will be places on cake\app\views\themed\default\layouts.
    While css and image files are in \app\webroot\themed\default\….

    This way user can pick theme (Eg. default) and CakePHP will magically do the rest.
    You only need to put files on proper folder.

    Don’t forget! On production server, /webroot is set as the DocumentRoot.
    So this way, we hide our php / cakephp script from public. Refer to this CakePHP Production Installation

    User can only see \webroot\… but not \views\…
    Anything inside views should NOT be visible to user.
    So don’t put images inside \views\…, put in \webroot\ instead.

Leave a Reply




Cannot find your answer here?
Feel free to get in touch and ask PHP-ist anything, just anything :)