-
Notifications
You must be signed in to change notification settings - Fork 333
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
ENH Improve type safety to support refactored template layer #3010
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -290,16 +290,6 @@ public function getMenu($level = 1) | |
return new ArrayList($visible); | ||
} | ||
|
||
/** | ||
* @return ArrayList<SiteTree> | ||
* @deprecated 5.4.0 Use getMenu() instead. You can continue to use $Menu in templates. | ||
*/ | ||
public function Menu($level) | ||
{ | ||
Deprecation::noticeWithNoReplacment('5.4.0', 'Use getMenu() instead. You can continue to use $Menu in templates.'); | ||
return $this->getMenu($level); | ||
} | ||
|
||
/** | ||
* Returns the default log-in form. | ||
* | ||
|
@@ -416,23 +406,23 @@ public function getViewer($action) | |
$action = $action === 'index' ? '' : '_' . $action; | ||
|
||
$templatesFound = []; | ||
// Find templates for the record + action together - e.g. Page_action.ss | ||
// Find templates for the record + action together - e.g. Page_action | ||
if ($this->dataRecord instanceof SiteTree) { | ||
$templatesFound[] = $this->dataRecord->getViewerTemplates($action); | ||
} | ||
|
||
// Find templates for the controller + action together - e.g. PageController_action.ss | ||
$templatesFound[] = SSViewer::get_templates_by_class(static::class, $action, Controller::class); | ||
// Find templates for the controller + action together - e.g. PageController_action | ||
$templatesFound[] = SSViewer::get_templates_by_class(static::class, $action ?? '', Controller::class); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
// Find templates for the record without an action - e.g. Page.ss | ||
// Find templates for the record without an action - e.g. Page | ||
if ($this->dataRecord instanceof SiteTree) { | ||
$templatesFound[] = $this->dataRecord->getViewerTemplates(); | ||
} | ||
|
||
// Find the templates for the controller without an action - e.g. PageController.ss | ||
$templatesFound[] = SSViewer::get_templates_by_class(static::class, "", Controller::class); | ||
// Find the templates for the controller without an action - e.g. PageController | ||
$templatesFound[] = SSViewer::get_templates_by_class(static::class, '', Controller::class); | ||
|
||
$templates = array_merge(...$templatesFound); | ||
return SSViewer::create($templates); | ||
return SSViewer::create($templates, $this->getTemplateEngine()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1454,7 +1454,7 @@ public function MetaComponents() | |
|
||
$tags['title'] = [ | ||
'tag' => 'title', | ||
'content' => $this->obj('Title')->forTemplate() | ||
'content' => $this->obj('Title')?->forTemplate() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
]; | ||
|
||
$generator = $this->getGenerator(); | ||
|
@@ -1601,7 +1601,7 @@ public function MetaTags($includeTitle = true) | |
|
||
$tagString = implode("\n", $tags); | ||
if ($this->ExtraMeta) { | ||
$tagString .= $this->obj('ExtraMeta')->forTemplate(); | ||
$tagString .= $this->obj('ExtraMeta')?->forTemplate(); | ||
} | ||
|
||
$this->extend('updateMetaTags', $tagString); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace SilverStripe\CMS\Tests\Controllers\ContentControllerTest; | ||
|
||
use SilverStripe\CMS\Controllers\ContentController; | ||
use SilverStripe\Dev\TestOnly; | ||
use SilverStripe\View\TemplateEngine; | ||
|
||
class DummyTemplateContentController extends ContentController implements TestOnly | ||
{ | ||
protected function getTemplateEngine(): TemplateEngine | ||
{ | ||
return new DummyTemplateEngine(); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
namespace SilverStripe\CMS\Tests\Controllers\ContentControllerTest; | ||
|
||
use SilverStripe\Dev\TestOnly; | ||
use SilverStripe\View\TemplateEngine; | ||
use SilverStripe\View\ViewLayerData; | ||
|
||
/** | ||
* A dummy template renderer that doesn't actually render any templates. | ||
*/ | ||
class DummyTemplateEngine implements TemplateEngine, TestOnly | ||
{ | ||
private string $output = '<html><head></head><body></body></html>'; | ||
|
||
private string|array $templates; | ||
|
||
public function __construct(string|array $templateCandidates = []) | ||
{ | ||
$this->templates = $templateCandidates; | ||
} | ||
|
||
public function setTemplate(string|array $templateCandidates): static | ||
{ | ||
$this->templates = $templateCandidates; | ||
return $this; | ||
} | ||
|
||
public function hasTemplate(string|array $templateCandidates): bool | ||
{ | ||
return true; | ||
} | ||
|
||
public function renderString(string $template, ViewLayerData $model, array $overlay = [], bool $cache = true): string | ||
{ | ||
return $this->output; | ||
} | ||
|
||
public function render(ViewLayerData $model, array $overlay = []): string | ||
{ | ||
return $this->output; | ||
} | ||
|
||
public function setOutput(string $output): void | ||
{ | ||
$this->output = $output; | ||
} | ||
|
||
/** | ||
* Returns the template candidates that were passed to the constructor or to setTemplate() | ||
*/ | ||
public function getTemplates(): array | ||
{ | ||
return $this->templates; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't needed anymore. It existed because you couldn't pass args into getters from a template using
$Menu(2)
syntax.I've resolved that - the args now get passed through to the getter.