/
SimpleView.php
73 lines (63 loc) · 1.82 KB
/
SimpleView.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
namespace Level14\Website2Feed;
/**
* Extends the Slim view with a few convenience methods and a simple, one level layout.
*
* The Slim app is injected with the setApp() method so that views can avoid using Slim::getInstance().
*/
class SimpleView extends \Slim\View {
/**
* @var \Slim\Slim
*/
protected $app;
/**
* Sets the current Slim app.
*
* @param \Slim\Slim $app
*/
public function setApp(\Slim\Slim $app) {
$this->app = $app;
}
/**
* The name of the layout file for the currently rendered view.
* Null, if no layout is needed.
*
* @var string
*/
private $layoutFile = null;
/**
* Adds a parent template (layout) to the view;
*
* @param string $file
*/
public function enableLayout($file = 'layout.php') {
$this->layoutFile = $file;
}
/**
* Title of the current HTML page. Only used if layout is enabled.
*
* @var string
*/
private $htmlTitle = '';
/**
* Sets the title for HTML pages. Only used if layout is enabled.
*
* @param type $title
*/
public function setHtmlTitle($title) {
$this->htmlTitle = $title;
}
protected /*override*/ function render($template, $data = null) {
$contents = parent::render($template, $data);
if (is_null($this->layoutFile)) {
return $contents;
}
else {
return parent::render('layout.php', array('contents' => $contents, 'title' => $this->htmlTitle));
}
}
public function urlWithApikeyFor($name, $params = [])
{
return $this->app->urlFor($name, $params) . '?apikey=' . $GLOBALS['config']['apikey'];
}
}