Repository navigation
Expand file tree
/
Copy pathSimpieView.php
More file actions
121 lines (102 loc) · 2.88 KB
/
Copy pathSimpieView.php
File metadata and controls
121 lines (102 loc) · 2.88 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
/**
* Simple template for tipi
*
* @author reeze <reeze.xia@gmail.com>
*/
class SimpieView
{
protected $layout;
protected $view;
protected $raw_view;
protected $file_type;
// when this passes to constructor, $view will be treated as raw text intead of parse it
const IS_RAW_TEXT = true;
const MARKDOWN_VIEW = 'markdown';
public function __construct($view, $layout=null, $raw_view=false, $file_type=null)
{
$this->view = $view;
$this->layout = $layout;
$this->raw_view = $raw_view;
$this->file_type = $file_type;
}
/**
* Render the view/view with layout
*
* @param array $params the params to extract to template
* @param bool $return is return or direct output the template
*
* @return mixed template's output or null
*/
public function render($params=array(), $return=false)
{
// view May direct output
if($this->raw_view) {
$view_result = $this->view;
}
else {
$view_result = $this->_render($this->view, $params, $this->layout || $return, $this->file_type);
}
if($this->layout) {
$params['layout_content'] = $view_result;
return $this->_render($this->layout, $params, $return, 'php'); // layout must be php
}
return $view_result;
}
/**
* Act likes include and Simpie's render method. used in template file to include partial
*
* @param string $partial_path the partial to include
* @param array $params the param to extract
*
* @return mixed template's output or null
*/
public static function include_partial($partial_path, $params=array(), $return=false, $file_type=null) {
$partial = new self($partial_path, null, null, $file_type);
return $partial->render($params, $return);
}
private function _render($view_path, $params=array(), $return=false, $file_type=null)
{
if(!file_exists($view_path)) {
throw new SimpieViewNotFoundException("View path:{$view_path} not found");
}
if(!$file_type) {
if($this->file_type) {
$file_type = $this->file_type;
}
else {
$file_type = substr($view_path, strrpos($view_path, '.') + 1);
}
}
$file_type = in_array($file_type, array('php', 'markdown')) ? $file_type : 'php';
return call_user_func(array($this, "_render_{$file_type}"), $view_path, $params, $return);
}
private function _render_php($view_path, $params=array(), $return=false)
{
if($return) ob_start();
// Simple extract
extract($params);
require $view_path;
if($return) {
return ob_get_clean();
}
}
private function _render_markdown($view_path, $params=array(), $return=false)
{
require_once "TipiMarkdownExt.php";
static $parser;
if (!isset($parser)) {
$parser_class = MARKDOWN_PARSER_CLASS;
$parser = new $parser_class;
}
# Transform text using parser.
$output = $parser->transform(file_get_contents($view_path));
if($return) {
return $output;
}
else {
echo $output;
}
}
}
class SimpieViewNotFoundException extends Exception {}