Skip to content

Commit

Permalink
Merge pull request #701 from cakephp/fix-paths
Browse files Browse the repository at this point in the history
Fix template override paths
  • Loading branch information
markstory authored Aug 3, 2020
2 parents 4f723a5 + 649ae7d commit 16d2816
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 7 deletions.
2 changes: 1 addition & 1 deletion docs/en/development.rst
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ If you wish to modify the default output produced by the "bake" command, you can
create your own bake templates in your application. This way does not use the
``--theme`` option in the command line when baking. The best way to do this is:

#. Create a new directory **/templates/bake/**.
#. Create a new directory **/templates/plugin/Bake/**.
#. Copy any templates you want to override from
**vendor/cakephp/bake/templates/bake/** to matching files in your
application.
Expand Down
11 changes: 5 additions & 6 deletions src/View/BakeView.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,7 @@ public function initialize(): void
public function render(?string $view = null, $layout = null): string
{
$viewFileName = $this->_getTemplateFileName($view);
$templateEventName = str_replace(
['.twig', DS],
['', '.'],
explode('templates' . DS . static::BAKE_TEMPLATE_FOLDER . DS, $viewFileName)[1]
);
$templateEventName = str_replace(DS, '.', $view);

$this->_currentType = static::TYPE_TEMPLATE;
$this->dispatchEvent('View.beforeRender', [$viewFileName]);
Expand Down Expand Up @@ -142,7 +138,10 @@ protected function _paths(?string $plugin = null, bool $cached = true): array
{
$paths = parent::_paths($plugin, false);
foreach ($paths as &$path) {
$path .= static::BAKE_TEMPLATE_FOLDER . DS;
// 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;
}
}

return $paths;
Expand Down
11 changes: 11 additions & 0 deletions tests/TestCase/View/BakeViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,4 +170,15 @@ public function testCustomRenderEvents()
$result
);
}

/**
* Ensure that application override templates don't have a double path in them.
*
* @return void
*/
public function testApplicationOverride()
{
$result = $this->View->render('Bake.override');
$this->assertSame("Application override.\n", $result);
}
}
1 change: 1 addition & 0 deletions tests/test_app/templates/plugin/Bake/override.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Application override.

0 comments on commit 16d2816

Please sign in to comment.