AD7six.com

Speed up your dev: write custom bake templates

28 November, 2007

Show comments

When I first started using cake, bake wasn't around. It took quite some time for me to decide to give it a go, but at that time what bake gave wasn't customizable, as such it's use was limited since it took nearly as much time to bake and edit the files as it did to just write the files (once you know what you are doing that is). Roll the clock forward a bit and bake allowed you to write your own custom templates. It, again, took quite some time for me to take note of the impact of this but it is in reality a big step forwards. Not only can new bakers use bake to get a working editable application, but you can also write your own templates and use bake to get exactly the code you want.

Shortcut: The files

Files which are discussed here are available from the Noswad Cakeforge project. I would have preferred to include the files here in the post, but my pretty-php script just can't handle l33t code that writes code and still output valid xhtml.

Preparation

To make the most of anything in life, takes a bit of preparation. A few minutes spent thinking at the beginning of a task/project can save hours of thinking/fixing later on. Here are a few of the things I thought about when re-evaluating how best to make use of bake:

  1. I only ever use bake for admin functions
  2. Standard baked models look fine
  3. Standard baked controllers don't follow my own pseudo conventions.
  4. For me, there is no code required in baked/standard controllers. It's all inherited from the app_controller
  5. Standard baked views don't match my own pseudo conventions to the point where they aren't useful.


It's a short list, so applying these observations to the bake process; if I put a standard app controller in place and write some appropriate view templates, I should then be able to bake the models, bake empty controllers bake the admin views and be very close to 'done'. Well that's the theory ;)

Be consistent

Although the focus of this post is to show how to write and use your own bake templates, I'll cover a few of my own pseudo conventions which are included in the example app controller and allow for writing less and doing more.

  1. Write generic/parametrizable methods in abstract classes, rather than writing specific methods in specific controllers
  2. Don't override any method unless you need to - call the parent if overridden and appropriate.
  3. Store the referrer in the session such that it doesn't matter how you get to a form, or how many times you submit bad data, on success you go back to where you came from.
  4. The primary data variable in the view is always $data.
  5. Pass the primary model name to the view so that views, if appropriate, can be reused.
  6. There is only one view for add and edit, named edit or admin_edit unless their needs are truely different.
  7. Admin indexes have dynamic filters.


Hopefully these points and the code in the download are clear, if not refer to this list whilst puzzling over what a bit of code does.

Write your app controller

If the code that cake gives you when baking a controller is what you want, you can skip this step of course. In the download is a standard app controller with no dependencies (i.e. you don't need any particular components/libraries/logic in place to use it). If it doesn't suit your coding style, adapt it, or as I said before if cake's baked controllers are exactly what you want just delete it.

Write your bake view templates

This, right here, is where the fun is. Here is the magic, this is what will save you time and give you that warm feeling when you bake an app and find you are virtually done. I mentioned some of my own pseudo-conventions above, and following them allows for simpler view files. That also means simpler bake templates. the process I used to write these templates is simple: Look at the view files you regularly write, optimize them if you can, and then consider how you would write code to output your standard views. Then, compare what vague thoughts you have formed with the bake view templates from the cake folder, and thrash something out.

After following that process myself I added to my convention list the following rules:

  1. Always, always use single quoted strings in real code.
  2. Use core helpers to their fullest.
  3. Always, always use double quoted strings to wrap real code in bake templates.
  4. Where practical in a bake template, wrap each line in php tags.

The results are in the download.

Continual Improvement

If you have successfully followed these steps, and now have your own custom view templates being pumped out whenever you use bake: congratulations! However don't get complacent, if you find you need to edit your views because your baked views don't match what you want or are missing some information - if at all possible don't edit the view, edit the bake template and rebake (take care not to overwrite other view files you might want to keep hold of). This way you'll continually improve your bake templates rather than continually diverge your desired code from what you get out of bake.

Wrapping up

Bake is good. Getting bake to give you exactly what you want rather than something to 'just get going' is better. Included in the download are a sample app controller and example templates for forms and index actions. I hope these files demonstrate how you can write your own bake templates and make the most of this great tool.

Bake on!