Skip to content

Commit 4c9cf21

Browse files
committed
Allow a callback as template
See #22
1 parent 76e0433 commit 4c9cf21

File tree

2 files changed

+20
-13
lines changed

2 files changed

+20
-13
lines changed

src/Cortex/Router/MatchingResult.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,15 @@ public function matched()
8686
}
8787

8888
/**
89-
* @return string
89+
* @return string|bool|callable
9090
*/
9191
public function template()
9292
{
9393
$template = $this->data['template'];
9494

95-
return (is_string($template) || $template === false) ? $template : '';
95+
return (is_string($template) || $template === false || is_callable($template))
96+
? $template
97+
: '';
9698
}
9799

98100
/**

src/Cortex/Router/ResultHandler.php

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function handle(MatchingResult $result, \WP $wp, $doParseRequest)
2828
$result = apply_filters('cortex.match.done', $result, $wp, $doParseRequest);
2929
$handlerResult = $doParseRequest;
3030

31-
if (! $result instanceof MatchingResult) {
31+
if (!$result instanceof MatchingResult) {
3232
return $result;
3333
}
3434

@@ -40,23 +40,28 @@ public function handle(MatchingResult $result, \WP $wp, $doParseRequest)
4040
$before = $this->buildCallback($result->beforeHandler());
4141
$after = $this->buildCallback($result->afterHandler());
4242
$template = $result->template();
43-
(is_string($template)) or $template = '';
4443
$vars = $result->vars();
4544
$matches = $result->matches();
4645

46+
if (is_callable($template)) {
47+
$template = $template($vars, $wp, $matches);
48+
}
49+
50+
(is_string($template) || $template === 'false') or $template = '';
51+
4752
do_action('cortex.matched', $result, $wp);
4853

4954
is_callable($before) and $before($vars, $wp, $template, $matches);
5055
is_callable($handler) and $handlerResult = $handler($vars, $wp, $template, $matches);
5156
is_callable($after) and $after($vars, $wp, $template, $matches);
52-
$template and $this->setTemplate($template);
57+
$this->setTemplate($template);
5358

5459
do_action('cortex.matched-after', $result, $wp, $handlerResult);
5560

5661
is_bool($handlerResult) and $doParseRequest = $handlerResult;
5762
$doParseRequest = apply_filters('cortex.do-parse-request', $doParseRequest);
5863

59-
if (! $doParseRequest) {
64+
if (!$doParseRequest) {
6065
remove_filter('template_redirect', 'redirect_canonical');
6166

6267
return false;
@@ -79,7 +84,7 @@ private function buildCallback($handler)
7984
$built = $handler;
8085
}
8186

82-
if (! $built && $handler instanceof ControllerInterface) {
87+
if (!$built && $handler instanceof ControllerInterface) {
8388
$built = function (array $vars, \WP $wp, $template) use ($handler) {
8489
return $handler->run($vars, $wp, $template);
8590
};
@@ -93,14 +98,14 @@ private function buildCallback($handler)
9398
*/
9499
private function setTemplate($template)
95100
{
96-
if (is_string($template)) {
101+
if (is_string($template) && $template) {
97102
$ext = apply_filters('cortex.default-template-extension', 'php');
98-
pathinfo($template, PATHINFO_EXTENSION) or $template .= '.'.ltrim($ext, '.');
103+
pathinfo($template, PATHINFO_EXTENSION) or $template .= '.' . ltrim($ext, '.');
99104
$template = is_file($template) ? $template : locate_template([$template], false);
100-
$template or $template = null;
105+
$template or $template = '';
101106
}
102-
103-
if (is_null($template)) {
107+
108+
if ($template === '' || !(is_string($template) || $template === false)) {
104109
return;
105110
}
106111

@@ -124,7 +129,7 @@ private function setTemplate($template)
124129
];
125130

126131
$returnTemplate = function () use ($template) {
127-
current_filter('template_include') and remove_all_filters('template_include');
132+
current_filter() === 'template_include' and remove_all_filters('template_include');
128133

129134
return $template;
130135
};

0 commit comments

Comments
 (0)