-
Notifications
You must be signed in to change notification settings - Fork 0
/
Template.class.php
executable file
·131 lines (97 loc) · 2.94 KB
/
Template.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
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
122
123
124
125
126
127
128
129
130
131
<?php
namespace Disco\classes;
/**
* This file holds the Template class.
*/
/**
* Template class.
* Extend \Twig_Environment to provide extra functionality..
*/
Class Template extends \Twig_Environment {
/**
* Get the default disco twig loader which enables extension-less template use.
*
*
* @param null|string|array $path The absolute path to the template directory, or an array of directories.
*
* @return \Disco\classes\TemplateLoader
*/
public static function defaultLoader($path = null){
if($path === null){
$path = \App::path() . '/' . trim(\App::config('TEMPLATE_PATH'),'/');
}//if
return new \Disco\classes\TemplateLoader($path);
}//defaultLoader
/**
* Construct the \Twig_Environment.
*
*
* @param null|\Twig_Loader_Filesystem|\Twig_Loader_Array $loader The loader in charge of finding twig
* templates.
* @param null|string|array $options Either a relative string path to the twig configuration options, an array
* of configuration options, or null for no options.
*/
public function __construct($loader = null, $options = 'app/config/twig.php'){
if($loader === null){
$loader = self::defaultLoader();
}//if
if($options !==null && !is_array($options)){
$options = require \App::path() . '/' . trim($options,'/');
}//if
parent::__construct($loader, $options);
$this->addExtension(new \Disco\twig\TwigExtension);
}//construct
/**
* Is the file a template that exists?
*
*
* @param string $name The template.
*
* @return boolean Whether the template exists.
*/
public function isTemplate($name){
try {
$this->loadTemplate($name);
return true;
} catch(\Twig_Error_Loader $e){
return false;
}//catch
}//isTemplate
/**
* Render a template resolving any aliases used in the template name path.
*
*
* @param string $name The template name.
* @param array $data The variables to pass into the template.
*
* @return string The rendered template.
*/
public function render($name,array $data = Array()){
return parent::render($name,$data);
}//render
/**
* Build a template and push its html onto the Views html stack.
*
*
* @param string $name The template name.
* @param array $data The data to pass the template.
*
* @return void
*/
public function with($name,$data=Array()){
\App::with('View')->html($this->build($name,$data));
}//with
/**
* Build a template.
*
*
* @param string $name The template name.
* @param array $data The data to pass the template.
*
* @return string
*/
public function build($name,$data=Array()){
return $this->render($name,$data);
}//build
}//Template
?>