1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[CakePHP4] bake で複数 twig を運用する際は -t オプションを使う

Last updated at Posted at 2021-10-08

答え

bin/cake bake template table_name -t theme_name

やっていたこと

bootstrap_cli.php で複数の Theme を利用していた。

config/bootstrap_cli.php
// template 生成用のテーマ (AdminLTE 専用)
Configure::write('Bake.theme', 'AdminLteTheme');

// Model 生成用のテーマ
Configure::write('Bake.theme', 'GenerationGapModelBaker');

オプションなしで bake で template 生成

vendor/cakephp/bake/templates/bake/Template 配下の twig が適用され、bootstrap_cli.php で Configure::write したテーマが適用されなかった。

bin/cake bake template table_name

原因

テーマは vendor/cakephp/bake/src/Command/TemplateCommand.phpTemplateRenderer($args->getOption('theme')); で最後に追加したテーマを適用する。
結果として別の twig、または vendor 配下の FW の twig が適用される。

vendor/cakephp/bake/src/Command/TemplateCommand.php
    public function getContent(Arguments $args, ConsoleIo $io, string $action, ?array $vars = null)
    {
        if (!$vars) {
            $vars = $this->_loadController($io);
        }

        if (empty($vars['primaryKey'])) {
            $io->error('Cannot generate views for models with no primary key');
            $this->abort();
        }

        // 最後に追加したテーマを適用
        $renderer = new TemplateRenderer($args->getOption('theme'));
        $renderer->set('action', $action);
        $renderer->set('plugin', $this->plugin);
        $renderer->set($vars);

        $indexColumns = 0;
        if ($action === 'index' && $args->getOption('index-columns') !== null) {
            $indexColumns = $args->getOption('index-columns');
        }
        $renderer->set('indexColumns', $indexColumns);

        return $renderer->generate("Bake.Template/$action");
    }

対応

テーマ名をオプションで指定する。

bin/cake bake template table_name -t theme_name

help 見よう

help をちゃんと読んでいなかった…
default と、選択可能なテーマ choices がわかる。

$ bin/cake bake template --help
(中略)
--theme, -t          The theme to use when baking code.
                     (default:
                     GenerationGapModelBaker)
                     (choices:
                     AdminLTE|Bake|GenerationGapModelBaker|Migrations)

複数テーマ利用例

plugins 配下の構成

bake 自動ファイル生成用に、複数の twig を plugins 配下に作成

  • template 用
  • model 用

実際に、AdminLTE テーマに対応した template 生成用の twig ファイルと、カスタマイズした model を生成する twig ファイルを共存した例です。
(GenerationGapModelBaker は現在は パッケージ化 してます)

$ pwd
~/www/app/plugins
$ pwd;find . | sort | sed '1d;s/^\.//;s/\/\([^/]*\)$/|--\1/;s/\/[^/|]*/|  /g'
|--AdminLteTheme
|  |--templates
|  |  |--bake
|  |  |  |--Template
|  |  |  |  |--add.twig
|  |  |  |  |--edit.twig
|  |  |  |  |--index.twig
|  |  |  |  |--view.twig
|--GenerationGapModelBaker
|  |--templates
|  |  |--bake
|  |  |  |--Model
|  |  |  |  |--entity.twig
|  |  |  |  |--extended_entity.twig
|  |  |  |  |--extended_table.twig
|  |  |  |  |--table.twig
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?