-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJade.php
86 lines (82 loc) · 2.88 KB
/
Jade.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
78
79
80
81
82
83
84
85
86
<?php
/**
* @author kylekatarnls
*/
class Jade
{
protected $CI;
protected $jade;
protected $view_path;
public function __construct(array $options = null)
{
if (is_null($options)) {
$options = defined('static::SETTINGS') ? ((array) static::SETTINGS) : array();
}
if (isset($options['view_path'])) {
$this->view_path = $options['view_path'];
unset($options['view_path']);
} else {
$this->view_path = APPPATH.'views';
}
if (isset($options['cache'])) {
if ($options['cache'] === true) {
$options['cache'] = APPPATH.'cache/jade';
}
if (!file_exists($options['cache']) && !mkdir($options['cache'], 0777, true)) {
throw new Exception('Cache folder does not exists and cannot be created.', 1);
}
}
$this->CI = &get_instance();
if (!class_exists('Jade\\Jade')) {
spl_autoload_register(function ($className) {
if (strpos($className, 'Jade\\') === 0) {
$path = __DIR__.DIRECTORY_SEPARATOR.str_replace('\\', DIRECTORY_SEPARATOR, $className).'.php';
if (file_exists($path)) {
include_once $path;
}
}
});
}
$this->jade = new Jade\Jade($options);
}
public function getEngine()
{
return $this->jade;
}
public function view($view, array $data = array(), $return = false)
{
if (is_array($view) || $view === true) {
$return = (bool) $data;
$data = $view;
$view = null;
}
if ($data === true) {
$data = array();
$return = true;
}
if (is_null($view)) {
$view = $this->router->class.DIRECTORY_SEPARATOR.$this->router->method;
}
if (!$this->jade) {
$this->settings();
}
$view = $this->view_path.DIRECTORY_SEPARATOR.$view.'.pug';
if (!file_exists($view)) {
$isIndex = (strtr('\\', '/', substr($view, -11)) === '/index.pug');
$view = $isIndex ? substr($view, 0, -11).'.pug' : substr($view, 0, -5).DIRECTORY_SEPARATOR.'index.pug';
if (!file_exists($view)) {
$view = $this->view_path.DIRECTORY_SEPARATOR.$view.'.jade';
if (!file_exists($view)) {
$isIndex = (strtr('\\', '/', substr($view, -11)) === '/index.jade');
$view = $isIndex ? substr($view, 0, -11).'.jade' : substr($view, 0, -5).DIRECTORY_SEPARATOR.'index.jade';
}
}
}
$data = array_merge($this->CI->load->get_vars(), $data);
if ($return) {
return $this->jade->render($view, $data);
} else {
echo $this->jade->render($view, $data);
}
}
}