Search code examples
cakephpcakephp-3.0cakephp-bakecake-bake

CakePHP Bake from different templates when using prefix


When I currently use ./cake.bat bake template Genres or ./cake.bat bake template Genres --prefix admin, then the templates are used from these locations:

cakephp\bake\src\Template\Bake\Template\view.twig
cakephp\bake\src\Template\Bake\Template\index.twig
cakephp\bake\src\Template\Bake\Template\add.twig
cakephp\bake\src\Template\Bake\Template\edit.twig

I want to different versions of all these templates when I bake with the admin prefix. I tried creating a Bake Theme.

  • I ran ./cake.bat bake plugin AdminTheme
  • Then I placed the desired template files in plugins/AdminTheme/templates/Bake/Template/.
  • Ran ./cake.bat bake template Genres --theme AdminTheme
  • Got Error: "AdminTheme" is not a valid value for --theme. Please use one of "Bake, Migrations, WyriHaximus/TwigView"

Solution

  • Prefixes won't affect the template source, the path is pretty much hardcoded in \Bake\Shell\Task\TemplateTask::getContent(). One way to uses different templates is to bake each action separately, and use the action argument, and optionally the alias argument to use the default name for the output, something along the lines of this:

    bin\cake bake template Genres admin_index index --prefix Admin
    

    That would use the Template/admin_index.ctp|twig file, and write to Template/Admin/Genres/index.ctp.

    As for your bake theme, the template folder structure in your theme plugin has to be:

    AdminTheme/src/Template/Bake/Template/
    

    not

    AdminTheme/templates/Bake/Template/
    

    Also make sure that your plugin is actually being loaded, ie check that $this->addPlugin('AdminTheme'); exists in your Application class' bootstrap() method, or for earlier versions Plugin::load('AdminTheme'); in your config/bootstrap.php file.

    See also Bake Cookbook > Extending Bake > Creating a Bake Theme