/
Layout.class.php
77 lines (73 loc) · 2.58 KB
/
Layout.class.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
74
75
76
77
<?php
Library::import('recess.framework.helpers.AssertiveTemplate');
Library::import('recess.framework.helpers.Buffer');
/**
* Layout is a style of AssertiveTemplate that allows child templates
* to 'extend' parent Layouts. Context is transferred from child to parent
* by matching variables that exist in the child and are registered
* as an input to the parent. Thus layouts must specify any required and
* optional inputs they expect to be passed.
*
* Parent layouts require a '.layout.php' extension.
*
* @author Kris
*/
class Layout extends AssertiveTemplate {
private static $parentStack = array();
private static $debugTraces = array();
/**
* Outputs a child template. Pass the template name (with extension) and
* an associative array context of variables to be passed to the child.
*
* @param string The filename of the template, with extension, relative to AssertiveTemplate paths.
* @param array The associative array of context the child template expects.
* @return boolean Returns true on success.
*/
public static function draw($template, $context) {
Buffer::to($body);
$context = self::includeTemplate($template, $context);
Buffer::end();
if(empty(self::$parentStack)) {
echo $body;
return true;
} else {
if(!isset($context['body'])) {
$context['body'] = $body;
}
while($parent = array_pop(self::$parentStack)) {
try{
$parentInputs = self::getInputs($parent, 'Layout');
}catch(RecessFrameworkException $e) {
// if(RecessConf::$mode == RecessConf::DEVELOPMENT) {
$trace = array_pop(self::$debugTraces);
throw new RecessErrorException('Extended layout does not exist.', 0, 0, $trace[0]['file'], $trace[0]['line'], $trace[0]['args']);
// } else {
// throw $e;
// }
}
$context = array_intersect_key($context, $parentInputs);
$context = self::includeTemplate($parent, $context);
}
// if(RecessConf::$mode == RecessConf::DEVELOPMENT) {
array_pop(self::$debugTraces);
// }
return true;
}
}
/**
* Used by child templates so indicate they 'extend' a parent layout which
* is to be included and assume all requested context from a child. Parent
* layouts are required to use a '.layout.php' extension.
*
* @param string The name of the layout being extended without the '.layout.php' extension.
*/
public static function extend($assertiveTemplate) {
array_push(self::$parentStack, $assertiveTemplate . '.layout.php');
//if(RecessConf::$mode == RecessConf::DEVELOPMENT) {
$trace = debug_backtrace();
array_pop($trace);
array_push(self::$debugTraces, $trace);
//}
}
}
?>