【问题标题】:CakePHP Bake from different templates when using prefix使用前缀时从不同的模板进行 CakePHP 烘焙
【发布时间】:2019-09-17 23:28:53
【问题描述】:

当我目前使用./cake.bat bake template Genres./cake.bat bake template Genres --prefix admin 时,模板是从这些位置使用的:

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

当我使用管理员前缀烘焙时,我想要所有这些模板的不同版本。我试过creating a Bake Theme

  • 我跑了./cake.bat bake plugin AdminTheme
  • 然后我把想要的模板文件放到plugins/AdminTheme/templates/Bake/Template/中。
  • ./cake.bat bake template Genres --theme AdminTheme
  • 得到Error: "AdminTheme" is not a valid value for --theme. Please use one of "Bake, Migrations, WyriHaximus/TwigView"

【问题讨论】:

    标签: cakephp cakephp-3.0 cakephp-bake cake-bake


    【解决方案1】:

    前缀不会影响模板源,路径在 \Bake\Shell\Task\TemplateTask::getContent() 中几乎是硬编码的。使用不同模板的一种方法是分别烘焙每个动作,并使用action 参数和可选的alias 参数来使用输出的默认名称,类似于以下内容:

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

    这将使用Template/admin_index.ctp|twig 文件,并写入Template/Admin/Genres/index.ctp

    对于你的烘焙主题,你的主题插件中的模板文件夹结构必须是:

    AdminTheme/src/Template/Bake/Template/
    

    不是

    AdminTheme/templates/Bake/Template/
    

    还要确保您的插件实际上正在加载,即检查 $this->addPlugin('AdminTheme'); 是否存在于您的 Application 类的 bootstrap() 方法中,或者对于早期版本的 Plugin::load('AdminTheme'); 是否存在于您的 config/bootstrap.php 文件中。

    另见Bake Cookbook > Extending Bake > Creating a Bake Theme

    【讨论】:

    • 你的两种方法都有效。我在使用 3.0 版本时看到了 4.0 版本的文档。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-13
    • 2020-08-25
    • 1970-01-01
    • 2016-01-28
    • 1970-01-01
    • 2013-02-06
    • 2015-04-14
    相关资源
    最近更新 更多