Baking views

Published on and tagged with bake  cakephp  view

In Faster baking of controllers with the bake shell script I showed you some shortcuts for baking controllers. In this article I am going to show you shortcuts for baking views.

Similar to baking a controller you can now specify the controller for which you want to bake views:

cake bake view Users

This will generate views for all actions in the specified controller (for delete() actions no view is generated).

If you want to bake only a single view, you can specify which view to bake with:

cake bake view Users index

This will generate the index view for the UsersController.

In both cases, bake uses the templates in /cake/console/libs/templates/views to generate the views. You can now override those templates or even define your own templates by creating view files in /app/vendors/shells/templates/views or in /vendors/shells/templates/views. For example, if you want to create a template for “info” views, you would have to create the file “info.ctp” in one of the aforementioned folders.

The statement to generate a view by using a certain template is imho a bit illogical:

cake bake view Users info index

This statement creates the “index” view by using the “info” template. I think it would be more logical if it would be the other way around…

Anyway, happy baking :)

8 comments baked

  • Dave

    Super useful, just a couple days too late for me… (Stupid me, built a shell script to do the same thing cause there’s NO documentation out there on the bake script.)

  • cakebaker

    @Dave: Oh, that’s bad luck… I hope it wasn’t just a waste of time and you learned something by writing your shell script.

  • Dave

    At least I can do more with the command line now. But yes, BAD luck indeed.

  • links for 2007-08-14 « Richard@Home

    […] cakebaker » Baking views Explains how to override the default scaffold code with your own (tags: cakephp bake scaffold) […]

  • Richard@Home

    Hi cakebaker.

    Firstly, thanks for putting together and maintaining such a useful cake resources :-)

    Secondly, your example shows how to bake views from templates. Is there any way to build controllers from templates too?

    I had a dig around in cake\console\libs\templates but couldn’t spot any templates for controllers.

  • cakebaker

    @Richard: Thanks for your comment!

    No, there are no templates for controllers, see cake/console/libs/tasks/controller.php

  • Richard@Home

    Thanks for confirming it. I did go and dig around further in the source and while doing so realised it is not something that would lend itself easily to templating.

  • cakebaker

    @Richard: Yes, that’s true, even though it would help to avoid hard-coding the entire template in the task.

Bake a comment




(for code please use <code>...</code> [no escaping necessary])

© daniel hofstetter. Licensed under a Creative Commons License