1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Andrew Coulton <[email protected]> |
4
|
|
|
* @copyright 2015 inGenerator Ltd |
5
|
|
|
* @license http://kohanaframework.org/license |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Ingenerator\KohanaView\Renderer; |
9
|
|
|
|
10
|
|
|
|
11
|
|
|
use Ingenerator\KohanaView\Renderer; |
12
|
|
|
use Ingenerator\KohanaView\ViewModel\PageContentView; |
13
|
|
|
use Ingenerator\KohanaView\ViewModel\PageLayoutView; |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Renders a PageContentView and - when appropriate - renders the generated output inside a PageLayoutView. By |
18
|
|
|
* default it will render the layout on normal requests (or when no request is present) but not on AJAX requests. |
19
|
|
|
* This behaviour can be customised by calling the setUseLayout method. |
20
|
|
|
* |
21
|
|
|
* For example, from a controller: |
22
|
|
|
* |
23
|
|
|
* public function action_login() |
24
|
|
|
* { |
25
|
|
|
* $layout = new DefaultPageLayout; |
26
|
|
|
* $content = new LoginView($layout); |
27
|
|
|
* $renderer = new PageLayoutRenderer(new HTMLRenderer, $this->request); |
28
|
|
|
* $this->response->body($renderer->render($content)); |
29
|
|
|
* } |
30
|
|
|
* |
31
|
|
|
* Obviously in real life the creation of the views and renderers would happen in your dependency container. |
32
|
|
|
* |
33
|
|
|
* @package Ingenerator\KohanaView\Renderer |
34
|
|
|
*/ |
35
|
|
|
class PageLayoutRenderer |
36
|
|
|
{ |
37
|
|
|
/** |
38
|
|
|
* @var bool Whether to force (or not force) embedding the content in the layout |
39
|
|
|
*/ |
40
|
|
|
protected $use_layout; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var Renderer |
44
|
|
|
*/ |
45
|
|
|
protected $view_renderer; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var \Request |
49
|
|
|
*/ |
50
|
|
|
protected $current_request; |
51
|
|
|
|
52
|
|
|
public function __construct(Renderer $view_renderer, \Request $current_request = NULL) |
53
|
|
|
{ |
54
|
|
|
$this->view_renderer = $view_renderer; |
55
|
|
|
$this->current_request = $current_request; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param PageContentView $content_view |
60
|
|
|
* |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
|
|
public function render(PageContentView $content_view) |
64
|
|
|
{ |
65
|
|
|
$content = $this->view_renderer->render($content_view); |
66
|
|
|
if ($this->shouldUseLayout()) { |
67
|
|
|
return $this->renderInLayout($content_view->var_page(), $content); |
68
|
|
|
} else { |
69
|
|
|
return $content; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return bool |
75
|
|
|
*/ |
76
|
|
|
protected function shouldUseLayout() |
77
|
|
|
{ |
78
|
|
|
if ($this->use_layout !== NULL) { |
79
|
|
|
return $this->use_layout; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
if ($this->current_request AND $this->current_request->is_ajax()) { |
|
|
|
|
83
|
|
|
return FALSE; |
84
|
|
|
} else { |
85
|
|
|
return TRUE; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @param PageLayoutView $layout |
91
|
|
|
* @param string $content |
92
|
|
|
* |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
protected function renderInLayout(PageLayoutView $layout, $content) |
96
|
|
|
{ |
97
|
|
|
$layout->setBodyHTML($content); |
98
|
|
|
|
99
|
|
|
return $this->view_renderer->render($layout); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Configure whether to always wrap the content in the layout (TRUE), never (FALSE) or automatically for |
104
|
|
|
* non-AJAX requests (NULL) |
105
|
|
|
* |
106
|
|
|
* @param bool $use_layout |
107
|
|
|
* |
108
|
|
|
* @return void |
109
|
|
|
*/ |
110
|
|
|
public function setUseLayout($use_layout) |
111
|
|
|
{ |
112
|
|
|
$this->use_layout = $use_layout; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
} |
116
|
|
|
|
PHP has two types of connecting operators (logical operators, and boolean operators):
and
&&
or
||
The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like
&&
, or||
.Let’s take a look at a few examples:
Logical Operators are used for Control-Flow
One case where you explicitly want to use logical operators is for control-flow such as this:
Since
die
introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined withthrow
at this point:These limitations lead to logical operators rarely being of use in current PHP code.