JsTemplate.php
[Pman.Core] / JsTemplate.php
1 <?php
2 /***
3  *
4  * usage:
5  *
6  * just need to add this to HTML
7  *
8  * <script src="{baseURL}/Core/JsTemplate.js">
9  *
10  * 
11  *
12  * 
13  *
14  * first part should return a list of files to include.
15  * $x = new Pman_Core_JsTemplate($cfg)
16  *
17  * $x->to
18  *
19  * second part should compile and deliver.
20  *
21  * 
22  *
23  * // should return {baseurl}/Pman/JsTemplate/mod/file
24  *
25  * 
26  *
27  *
28  */
29 require_once 'Pman.php';
30
31 class Pman_Core_JsTemplate extends Pman {
32     
33     
34     var $modDir = false;
35     
36     function getAuth()
37     {
38         parent::getAuth();
39         return true;
40          
41     }
42      
43     function get()
44     {
45         // get the modules.
46         header('Content-type: text/javascript');
47         
48         $ff = HTML_FlexyFramework::get()->HTML_Template_Flexy;
49         
50         $ar = explode(PATH_SEPARATOR, $ff['templateDir']);
51         
52         
53         foreach($ar as $mod) {
54             $dir =   dirname($mod) . '/jtemplates';
55             if (!file_exists($dir)) {
56                 echo '// missing directory '. htmlspecialchars($dir) ."\n";
57             }
58             // got a directory..
59             foreach(glob("$dir/*.html") as $fn) {
60                 var_Dump($fn);
61                 
62
63             }
64   //              testing..
65 //new HTML_FlexyFramework_JsTemplate('/home/alan/gitlive/web.mtrack/MTrackWeb/jtemplates/TimelineTicket.html', 'Pman.template.TimelineTicket');
66             
67             
68             
69         }
70         exit;
71         
72         
73         
74     }
75     
76     
77     function compile($fn, $name)
78     {
79         // cached? - check file see if we have cached contents.
80         
81         
82         $contents = file_get_contents($fn);
83         $ar = preg_split('/(\{[^\}]+})/', $contents, -1, PREG_SPLIT_DELIM_CAPTURE);
84         //echo '<PRE>' . htmlspecialchars(print_r($ar,true));
85         
86         $ret = array();
87         
88         $ret[] = "var $name = function(t) {\n    var ret=[];\n";
89         $indent = 1;
90         foreach($ar as $item) {
91             $in = str_repeat("    ", $indent);
92             
93             //var_Dump(substr($item,-3,2));
94             switch(true) {
95                 case (!strlen($item)):
96                     continue;
97                 
98                 case ($item[0] != '{'):
99                     if (!strlen(trim($item))) {
100                         continue;
101                     }
102                     $ret[] = $in . "ret+= ". json_encode($item) . ";";
103                     continue;
104                 
105                 case (substr($item,1,3) == 'if('):
106                     $ret[] = $in . substr($item,1,-1) . ' {';
107                     $indent++;
108                     continue;
109                 
110                 case (substr($item,1,4) == 'end:'):
111                     $indent--;
112                     $in = str_repeat("    ", $indent);
113                     $ret[] = $in . "}";
114                     continue;
115                 
116                 case (substr($item,1,7) == 'return:'):
117                     $ret[] = $in . "return;";
118                     continue;
119                 
120                 case (substr($item,1,9) == 'function:'):
121                     $indent++;
122                     $def  = substr($item,10,-1) ;
123                     list($name,$body) = explode('(', $def, 2);
124                     
125                     
126                     $ret[] = $in . "var $name = function (" .  $body  . '{';
127                     continue;
128                 
129                 default:
130                     if (substr($item,-3,2) == ':h') {
131                         $ret[] = $in . "ret += ".  substr($item,1,-3) . ';';
132                         continue;
133                     }
134                     $ret[] = $in . "ret += Roo.util.Format.htmlEncode(".  substr($item,1,-1).');';
135                     continue;
136                 
137             }
138             
139             
140         }
141         $in = str_repeat("    ", $indent);
142         $ret[] = $in .  "return ret.join('');\n}\n";
143         
144         echo '<PRE>' . htmlspecialchars(implode("\n",$ret));
145         
146         
147         
148     }
149     
150     
151     
152 }
153
154 // 
155