Skip to content

Commit 38dfc80

Browse files
committed
Refactoring the Efficiently\JqueryLaravel\ControllerAdditions::render() method.
1 parent 3e4c01d commit 38dfc80

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

src/Efficiently/JqueryLaravel/ControllerAdditions.php

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -49,41 +49,42 @@ public function render($view, $data = [], $options = null)
4949
if (! ends_with($view, '_'.$format) && view()->exists($view.'_'.$format)) {
5050
$view = $view.'_'.$format;
5151
} else {
52-
// inline view
52+
// Inline view
5353
$view = Blade::compileString($view);
5454
}
5555
}
56+
if ($format === 'html') {
57+
$format = null; // if `null`, it uses the 'html' format by default
58+
}
59+
// If the format isn't 'html' and no custom layout is provided
60+
// The default layout should not be use
61+
if ($format && is_null($layout)) {
62+
$layout = false;
63+
}
5664
}
5765

5866
if (is_string($view) && view()->exists($view)) {
5967
// Transform the $view string path to a View object
6068
$view = view($view, $data);
6169
}
62-
63-
// short circuit
64-
if ($format) {
65-
$response = response($view, $options['status'], $options['headers']);
66-
$response->header('Content-Type', Request::getMimeType($format));
67-
68-
return $response;
69-
}
70-
71-
if (! is_null($layout)) {
72-
$this->layout = $layout;
73-
$this->setupLayout();
74-
}
75-
7670
$render = $view;
77-
if ($this->layout) {
78-
$this->layout->content = $view;
71+
72+
$this->setupLayout($layout);
73+
if ($this->layout && $this->layout->getName() !== $view->getName()) {
74+
$this->layout->with('content', $render);
7975
$render = $this->layout;
8076
}
8177

8278
if (response()->hasMacro('makeWithTurbolinks')) {
83-
return response()->makeWithTurbolinks($render, $options);
79+
$response = response()->makeWithTurbolinks($render, $options);
80+
} else {
81+
$response = response($render, $options['status'], $options['headers']);
82+
}
83+
if ($format) {
84+
$response->header('Content-Type', Request::getMimeType($format));
8485
}
8586

86-
return response()->make($render, $options['status'], $options['headers']);
87+
return $response;
8788
}
8889

8990
/**
@@ -105,12 +106,13 @@ public function redirectTo($path, $options = [])
105106

106107
/**
107108
* Setup the layout used by the controller.
108-
*
109+
* @param string|bool|\Illuminate\View\View $layout Custom layout
109110
* @return void
110111
*/
111-
protected function setupLayout()
112+
protected function setupLayout($layout = null)
112113
{
113-
if ($this->layout) {
114+
$this->layout = !is_null($layout) ? $layout : $this->layout;
115+
if ($this->layout && is_string($this->layout) && view()->exists($this->layout)) {
114116
$this->layout = view($this->layout);
115117
}
116118
}

0 commit comments

Comments
 (0)