Cake Bake: Custom Templates “Deluxe”

You might have read my almost two year old article about custom bake templates.
Much changed since then. In Cake as well in my user-land code.

Note: The "setup" bake theme is written for 2.x but can easily be backported to 1.3 manually.


We probably all know how xxxxxxx powerful the Bake tool is. It is all about speed for the first outline of a project.
So the faster the first CRUD controllers are baked the sooner we can dig into the details.
But speed itself is not the only crucial part. If we have a model/table with a lot of meta fields that should not get baked we would have to remove them manually from index/view/form every time we re-bake. That’s not really something you want to waste your time with.

Skip Fields

With some custom template like mine you can skip certain fields for baking. Some like 'password', 'slug', 'lft', 'rght' are automatically skipped by my template. But you can also define additional fields yourself:

class Deadline extends AppModel {
    public $scaffoldSkipFields = array('note');

So the textarea field "note" will not be outputted in your ctp files.

An additional feature I want to implement is that you can specify the actions it should skip:

public $scaffoldSkipFields = array('note'=>array('prefix'=>'', 'actions'=>array('index')));

This would mean that it skips this field for all non-prefixed ctps as well as all indexes.

public $scaffoldSkipFields = array('note'=>array('prefix'=>'admin')); // skip for all admin views

This will be included shortly.

Default Values

class User extends AppModel {
    public $scaffoldDefaultValues = array('role_id' => 'ROLE_USER', 'status' => 'User::STATUS_ACTIVE', 'level'=>1, 'text'=>'\'foo\'');

Which would result in this inside the controller action add:

if ($this->request->is('post')) {
} else {
    $this->request->data['User']['role_id'] = ROLE_USER;
    $this->request->data['User']['status'] = User::STATUS_ACTIVE;
    $this->request->data['User']['level'] = 1;
    $this->request->data['User']['text'] = 'foo'; 

This will have the above default values set up for adding a new user.

PS: Why not in the form itself? It is always better to put default values as a part of the logic into the controller and outside of the view layer.

Enum support

This article describes how to setup enums in CakePHP the easy way. Do this after you bake the model and BEFORE you bake controller and views. Then you will end up with ctps including all the Enum things automatically like a dropdown box for forms and translated values for index and view.

Smarter theme

We already covered a few basic field types before:

} elseif ($schema[$field]['type'] == 'date') {
    // formatted date
} elseif ($schema[$field]['type'] == 'boolean') {
    // yesNo image
} elseif ($schema[$field]['type'] == 'text') {
    // nl2br(h())

I also added decimals for currencies and normal float values:

} elseif ($schema[$field]['type'] == 'float' && strpos($schema[$field]['length'], ',2') !== false) {
    echo "\t\t<dd>\n\t\t\t<?php echo \$this->Numeric->money(\${$singularVar}['{$modelClass}']['{$field}']); ?>\n\t\t</dd>\n";
} elseif ($schema[$field]['type'] == 'float' && strpos($schema[$field]['length'], ',') !== false) {
    echo "\t\t<dd>\n\t\t\t<?php echo \$this->Numeric->format(\${$singularVar}['{$modelClass}']['{$field}']); ?>\n\t\t</dd>\n";

Switch Numeric with Number if you use your own helper. This will print a nicely formatted currency value instead of the default decimal value.


The files are in the Templates folder of my setup plugin.

Since my plugin theme might contain stuff you cannot and don’t wont to use you can cherrypick the above "tricks" for your own custom bake theme.

Now you know how you can further customize your themes to make baking even more fun as it already is 🙂

0.00 avg. rating (0% score) - 0 votes

1 Comment

  1. Hello thanks for very useful tutorial
    But how i do this in controller, for example if found field name image, file. In the action controller is added upload function ?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.