]> git.bts.cx Git - cx.git/blob - cx/lib/template.php
Moved how assets get served
[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) {
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         header('Content-Type: ' . $mime_type);
29         header('Content-Length: ' . filesize($full_path));
30
31         $fp = fopen($full_path, 'rb');
32         fpassthru($fp);
33         fclose($fp);
34 }
35
36 function cx_template_render($class, $name, $variables = null) {
37         global $cx_template_base, $cx_template_content;
38         
39         $base_template = null;
40
41         $output = '';
42         while ($name != null) {
43                 $segments = array(CX_PATH, 'templates', $class, 'pages', $name . '.php');
44                 $path = join(DIRECTORY_SEPARATOR, $segments);
45                 
46                 $base_template = null;
47                 $base_template_variables = null;
48
49                 $cx_template_base_previous = $cx_template_base;
50                 $cx_template_base = function($name, $base_variables) use (&$base_template, &$base_template_variables) {
51                         $base_template = $name;
52                         $base_template_variables = $base_variables;
53                 };
54
55                 $cx_template_content_previous = $cx_template_content;
56                 $cx_template_content = function() use ($output) {
57                         return $output;
58                 };
59
60                 cx_require('lib', 'url.php'); // For templates
61
62                 if ($variables != null) {
63                         extract($variables);
64                 }
65
66                 ob_start();
67                 include($path);
68                 $output = ob_get_contents();
69                 ob_end_clean();
70
71                 $cx_template_base = $cx_template_base_previous;
72                 $cx_template_content = $cx_template_content_previous;
73
74                 $name = $base_template;
75                 $variables = $base_template_variables;
76         }
77
78         return $output;
79 }
80
81 function cx_template_base($name, $variables = null) {
82         global $cx_template_base;
83         $cx_template_base($name, $variables);
84 }
85
86 function cx_template_content() {
87         global $cx_template_content;
88         return $cx_template_content();
89 }