1
|
|
|
<?php |
2
|
|
|
namespace JayaCode\Framework\Core\View\Template; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Class BasicTemplate |
6
|
|
|
* @package JayaCode\Framework\Core\View\Template |
7
|
|
|
*/ |
8
|
|
|
class BasicTemplate extends Template |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var |
12
|
|
|
*/ |
13
|
|
|
protected $parent; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @return string |
17
|
|
|
*/ |
18
|
|
|
public function buildScript() |
19
|
|
|
{ |
20
|
|
|
$fileCache = $this->cacheDir.str_replace(["/", "."], "_", $this->fileTemplate); |
21
|
|
|
if ($this->cacheDir && file_exists($fileCache) && empty($this->contentParent)) { |
|
|
|
|
22
|
|
|
return $this->script = file_get_contents($fileCache); |
|
|
|
|
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
$this->script = $this->converter->build(file_get_contents($this->locTemplate)); |
26
|
|
|
|
27
|
|
|
if ($this->cacheDir && empty($this->contentParent)) { |
|
|
|
|
28
|
|
|
file_put_contents($fileCache, $this->script); |
29
|
|
|
} |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return mixed|string |
34
|
|
|
*/ |
35
|
|
|
public function render() |
36
|
|
|
{ |
37
|
|
|
if (!$this->script) { |
38
|
|
|
$this->buildScript(); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
extract($this->vars->all()); |
|
|
|
|
42
|
|
|
ob_start(); |
43
|
|
|
eval("?>" . $this->script); |
|
|
|
|
44
|
|
|
$result = ob_get_contents(); |
45
|
|
|
ob_end_clean(); |
46
|
|
|
|
47
|
|
|
if ($this->parent) { |
48
|
|
|
$fileParent = $this->parent.Template::$extension; |
49
|
|
|
$parent = new BasicTemplate($this->locView, $fileParent, $this->variableCollector, $this->converter); |
50
|
|
|
$parent->setContentParent($this->contentParent); |
51
|
|
|
return $parent->render(); |
52
|
|
|
} |
53
|
|
|
return $result; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param $name |
58
|
|
|
*/ |
59
|
|
|
public function setParent($name) |
60
|
|
|
{ |
61
|
|
|
$this->parent = $name; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var array |
66
|
|
|
*/ |
67
|
|
|
protected $contentParent = []; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @return array |
71
|
|
|
*/ |
72
|
|
|
public function getContentParent() |
73
|
|
|
{ |
74
|
|
|
return $this->contentParent; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param array $contentParent |
79
|
|
|
*/ |
80
|
|
|
public function setContentParent($contentParent) |
81
|
|
|
{ |
82
|
|
|
$this->contentParent = $contentParent; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: