]> git.bts.cx Git - cx.git/blob - cx/lib/template.php
Added page ordering
[cx.git] / cx / lib / template.php
1 <?php
2
3 cx_require('third_party', 'mime', 'mime_types.php');
4
5 function cx_template_content_path($class, $path) {
6         $segments = array(CX_PATH, 'templates', $class, 'assets', $path);
7         $path = join(DIRECTORY_SEPARATOR, $segments);
8         return $path;
9 }
10
11 function cx_template_has_content($class, $path) {
12         return file_exists(cx_template_content_path($class, $path));
13 }
14
15 function cx_template_output_content($class, $path, $cache_age = 30 * 86400) {
16         $full_path = cx_template_content_path($class, $path);
17
18         $extension = pathinfo($path, PATHINFO_EXTENSION);
19
20         if (isset(MIME_REVERSE_MAP[$extension])) {
21                 $mime_type = MIME_REVERSE_MAP[$extension][0];
22         } else {
23                 $finfo = finfo_open(FILEINFO_MIME_TYPE);
24                 $mime_type = finfo_file($finfo, $full_path);
25                 //finfo_close($finfo); - Deprecated
26         }
27
28         $file_size = filesize($full_path);
29         $file_modified_time = filemtime($full_path);
30
31         header('Content-Type: ' . $mime_type);
32         header('Content-Length: ' . $file_size);
33         header('Cache-Control: public, max-age=604800');
34         header('Expires: ' . gmdate('D, d M Y H:i:s', time() + $cache_age) . ' GMT');
35         header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $file_modified_time) . ' GMT');
36
37         if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
38                 $request_modification = strtotime($_SERVER['HTTP_IF_MODIFIED_SINCE']);
39                 if ($request_modification >= $file_modified_time) {
40                         http_response_code(304);
41                         return;
42                 }
43         }
44
45         $fp = fopen($full_path, 'rb');
46         fpassthru($fp);
47         fclose($fp);
48 }
49
50 function cx_template_render($class, $name, $variables = null) {
51         global $cx_template_base, $cx_template_content;
52         
53         $base_template = null;
54
55         $output = '';
56         while ($name != null) {
57                 $segments = array(CX_PATH, 'templates', $class, 'pages', $name . '.php');
58                 $path = join(DIRECTORY_SEPARATOR, $segments);
59                 
60                 $base_template = null;
61                 $base_template_variables = null;
62
63                 $cx_template_base_previous = $cx_template_base;
64                 $cx_template_base = function($name, $base_variables) use (&$base_template, &$base_template_variables) {
65                         $base_template = $name;
66                         $base_template_variables = $base_variables;
67                 };
68
69                 $cx_template_content_previous = $cx_template_content;
70                 $cx_template_content = function() use ($output) {
71                         return $output;
72                 };
73
74                 cx_require('lib', 'url.php'); // For templates
75
76                 if ($variables != null) {
77                         extract($variables);
78                 }
79
80                 ob_start();
81                 include($path);
82                 $output = ob_get_contents();
83                 ob_end_clean();
84
85                 $cx_template_base = $cx_template_base_previous;
86                 $cx_template_content = $cx_template_content_previous;
87
88                 $name = $base_template;
89                 $variables = $base_template_variables;
90         }
91
92         return $output;
93 }
94
95 function cx_template_base($name, $variables = null) {
96         global $cx_template_base;
97         $cx_template_base($name, $variables);
98 }
99
100 function cx_template_content() {
101         global $cx_template_content;
102         return $cx_template_content();
103 }