|
3 | 3 | namespace SilverStripe\Control;
|
4 | 4 |
|
5 | 5 | use SilverStripe\Core\ClassInfo;
|
| 6 | +use SilverStripe\Core\Injector\Injector; |
6 | 7 | use SilverStripe\Dev\Debug;
|
| 8 | +use SilverStripe\Model\ModelData; |
7 | 9 | use SilverStripe\ORM\FieldType\DBHTMLText;
|
8 | 10 | use SilverStripe\Security\Member;
|
9 | 11 | use SilverStripe\Security\Security;
|
10 | 12 | use SilverStripe\View\SSViewer;
|
| 13 | +use SilverStripe\View\TemplateEngine; |
11 | 14 | use SilverStripe\View\TemplateGlobalProvider;
|
12 | 15 |
|
13 | 16 | /**
|
@@ -87,6 +90,8 @@ class Controller extends RequestHandler implements TemplateGlobalProvider
|
87 | 90 | 'handleIndex',
|
88 | 91 | ];
|
89 | 92 |
|
| 93 | + protected ?TemplateEngine $templateEngine = null; |
| 94 | + |
90 | 95 | public function __construct()
|
91 | 96 | {
|
92 | 97 | parent::__construct();
|
@@ -400,7 +405,7 @@ public function getViewer($action)
|
400 | 405 | $templates = array_unique(array_merge($actionTemplates, $classTemplates));
|
401 | 406 | }
|
402 | 407 |
|
403 |
| - return SSViewer::create($templates); |
| 408 | + return SSViewer::create($templates, $this->getTemplateEngine()); |
404 | 409 | }
|
405 | 410 |
|
406 | 411 | /**
|
@@ -452,9 +457,10 @@ protected function definingClassForAction($action)
|
452 | 457 | }
|
453 | 458 |
|
454 | 459 | $class = static::class;
|
455 |
| - while ($class != 'SilverStripe\\Control\\RequestHandler') { |
| 460 | + $engine = $this->getTemplateEngine(); |
| 461 | + while ($class !== RequestHandler::class) { |
456 | 462 | $templateName = strtok($class ?? '', '_') . '_' . $action;
|
457 |
| - if (SSViewer::hasTemplate($templateName)) { |
| 463 | + if ($engine->hasTemplate($templateName)) { |
458 | 464 | return $class;
|
459 | 465 | }
|
460 | 466 |
|
@@ -486,17 +492,25 @@ public function hasActionTemplate($action)
|
486 | 492 | $parentClass = get_parent_class($parentClass ?? '');
|
487 | 493 | }
|
488 | 494 |
|
489 |
| - return SSViewer::hasTemplate($templates); |
| 495 | + $engine = $this->getTemplateEngine(); |
| 496 | + return $engine->hasTemplate($templates); |
| 497 | + } |
| 498 | + |
| 499 | + public function renderWith($template, ModelData|array $customFields = []): DBHTMLText |
| 500 | + { |
| 501 | + // Ensure template engine is used, unless the viewer was already explicitly instantiated |
| 502 | + if (!($template instanceof SSViewer)) { |
| 503 | + $template = SSViewer::create($template, $this->getTemplateEngine()); |
| 504 | + } |
| 505 | + return parent::renderWith($template, $customFields); |
490 | 506 | }
|
491 | 507 |
|
492 | 508 | /**
|
493 | 509 | * Render the current controller with the templates determined by {@link getViewer()}.
|
494 | 510 | *
|
495 | 511 | * @param array $params
|
496 |
| - * |
497 |
| - * @return string |
498 | 512 | */
|
499 |
| - public function render($params = null) |
| 513 | + public function render($params = null): DBHTMLText |
500 | 514 | {
|
501 | 515 | $template = $this->getViewer($this->getAction());
|
502 | 516 |
|
@@ -735,4 +749,12 @@ public static function get_template_global_variables()
|
735 | 749 | 'CurrentPage' => 'curr',
|
736 | 750 | ];
|
737 | 751 | }
|
| 752 | + |
| 753 | + protected function getTemplateEngine(): TemplateEngine |
| 754 | + { |
| 755 | + if (!$this->templateEngine) { |
| 756 | + $this->templateEngine = Injector::inst()->create(TemplateEngine::class); |
| 757 | + } |
| 758 | + return $this->templateEngine; |
| 759 | + } |
738 | 760 | }
|
0 commit comments