Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

overwrite bake templates #384

Open
tgoeminne opened this issue May 11, 2023 · 7 comments
Open

overwrite bake templates #384

tgoeminne opened this issue May 11, 2023 · 7 comments

Comments

@tgoeminne
Copy link

I have copied and adjusted the files from BootstrapUI/bake/Template to templates/plugin/BootstrapUI/bake/Template/index.twig and adjusted them

But it does not seem to work.

@tgoeminne
Copy link
Author

I done the same thing to overwrite the layout and that works, but the bake templates don't...

@ndm2
Copy link
Collaborator

ndm2 commented May 11, 2023

Using plugin templates to override bake templates requires to use themes, and theme templates have a higher priority than application templates, hence you cannot override them.

One option would be to copy all files from vendor/friendsofcake/bootstrap-ui/templates/bake/ into templates/plugin/Bake/ (without the bake subdirectory, that's an error in the docs), make your modifications, and then run bake without -t BootstrapUI. Not great, but it is what it is.

Another option would be to bake templates individually and use aliases, eg create templates/plugin/Bake/Template/index_override.twig and then run bake template ControllerName index_override index -t BootstrapUI.

@tgoeminne
Copy link
Author

Normally you want to override a plugins templates like cakeDC, BootstrapUI,... you can copy them to the templates folder under templates/plugin/vendorname/ then it will use the files from that folder. Only the way Bake implements it, it will not use the overriden templates, but it will use the template from the plugin itself. Yes, you can create a new plugin and place the templates there and then use the other plugin/theme. But why does bake not use the correct template file like the regular template system....

@ndm2
Copy link
Collaborator

ndm2 commented May 12, 2023

As I said, bake templates are provided by plugins using CakePHP's theme functionality, and that's simply how themes work, they override the application/plugin level templates, which will in turn become fallbacks.

I guess it should be possible to add support for special application level overrides that win specifically over bake themes, you may want to create an issue over in the Bake repo.

@tgoeminne
Copy link
Author

It would appear you not understand the problem with bake...

@tgoeminne
Copy link
Author

its not application level, its about override a plugins template, hence put the template in templates/plugin/BootstrapUI.

But you are right the problem is with the bake plugin...

@ndm2
Copy link
Collaborator

ndm2 commented May 13, 2023

I understand what you're trying to override, when I'm saying "application level override" then I don't mean to override an application template, but simply that the override for the BootstrapUI plugin/theme template is defined in the application level templates.

Unless I'm misunderstanding what you're asking for, I'm very much aware of what the "problem" in bake is. I don't want to sound like a broken record, but it's that theme paths will win over everything else.

Go open vendor/cakephp/bake/src/View/BakeView.php, change the _paths() method as follows:

protected function _paths(?string $plugin = null, bool $cached = true): array
{
    $paths = parent::_paths($plugin, false);
    foreach ($paths as &$path) {
        // Append 'bake' to all directories that aren't the application override directory.
        if (strpos($path, 'plugin' . DS . 'Bake') === false) {
            $path .= static::BAKE_TEMPLATE_FOLDER . DS;
        }
    }

    // Prepend application level override paths for current bake theme.
    if (!empty($this->theme)) {
        $themePlugin = \Cake\Utility\Inflector::camelize($this->theme);

        $themeOverridePaths = [];
        $templatePaths = \Cake\Core\App::path(static::NAME_TEMPLATE);

        foreach ($templatePaths as $templatePath) {
            $themeOverridePaths[] = $templatePath
                . static::PLUGIN_TEMPLATE_FOLDER
                . DIRECTORY_SEPARATOR
                . $themePlugin
                . DIRECTORY_SEPARATOR
                . static::BAKE_TEMPLATE_FOLDER
                . DIRECTORY_SEPARATOR;
        }

        array_unshift($paths, ...$themeOverridePaths);
    }

    return $paths;
}

and then try baking again with -t BootstrapUI and your modified template located in

templates/plugin/BootstrapUI/bake/Template/index.twig

I'm somewhat confident that you'll see that what you're trying to do will suddenly work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants