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
30 class Pman_Core_JsTemplate extends Pman {
31     
32     function getAuth()
33     {
34         parent::getAuth();
35         return true;
36          
37     }
38      
39     function get()
40     {
41         // get the modules.
42         header('Content-type: text/javascript');
43         
44         $mods = $this->modulesList();
45         foreach($mods as $mod) {
46             $dir =   $this->rootDir.'/Pman/'. $mod . '/jtemplates';
47             if (!file_exists($dir)) {
48                 echo '// missing directory '. htmlspecialchars($dir) ."\n";
49             }
50             // got a directory..
51             foreach(glob("$dir/*.html") as $fn) {
52                 var_Dump($fn);
53                 
54
55             }
56   //              testing..
57 //new HTML_FlexyFramework_JsTemplate('/home/alan/gitlive/web.mtrack/MTrackWeb/jtemplates/TimelineTicket.html', 'Pman.template.TimelineTicket');
58             
59             
60             
61         }
62             
63         
64         
65         
66     }
67     
68     
69     function compile($fn, $name)
70     {
71         // cached? - check file see if we have cached contents.
72         
73         
74         $contents = file_get_contents($fn);
75         $ar = preg_split('/(\{[^\}]+})/', $contents, -1, PREG_SPLIT_DELIM_CAPTURE);
76         //echo '<PRE>' . htmlspecialchars(print_r($ar,true));
77         
78         $ret = array();
79         
80         $ret[] = "var $name = function(t) {\n    var ret=[];\n";
81         $indent = 1;
82         foreach($ar as $item) {
83             $in = str_repeat("    ", $indent);
84             
85             //var_Dump(substr($item,-3,2));
86             switch(true) {
87                 case (!strlen($item)):
88                     continue;
89                 
90                 case ($item[0] != '{'):
91                     if (!strlen(trim($item))) {
92                         continue;
93                     }
94                     $ret[] = $in . "ret+= ". json_encode($item) . ";";
95                     continue;
96                 
97                 case (substr($item,1,3) == 'if('):
98                     $ret[] = $in . substr($item,1,-1) . ' {';
99                     $indent++;
100                     continue;
101                 
102                 case (substr($item,1,4) == 'end:'):
103                     $indent--;
104                     $in = str_repeat("    ", $indent);
105                     $ret[] = $in . "}";
106                     continue;
107                 
108                 case (substr($item,1,7) == 'return:'):
109                     $ret[] = $in . "return;";
110                     continue;
111                 
112                 case (substr($item,1,9) == 'function:'):
113                     $indent++;
114                     $def  = substr($item,10,-1) ;
115                     list($name,$body) = explode('(', $def, 2);
116                     
117                     
118                     $ret[] = $in . "var $name = function (" .  $body  . '{';
119                     continue;
120                 
121                 default:
122                     if (substr($item,-3,2) == ':h') {
123                         $ret[] = $in . "ret += ".  substr($item,1,-3) . ';';
124                         continue;
125                     }
126                     $ret[] = $in . "ret += Roo.util.Format.htmlEncode(".  substr($item,1,-1).');';
127                     continue;
128                 
129             }
130             
131             
132         }
133         $in = str_repeat("    ", $indent);
134         $ret[] = $in .  "return ret.join('');\n}\n";
135         
136         echo '<PRE>' . htmlspecialchars(implode("\n",$ret));
137         
138         
139         
140     }
141     
142     
143     
144 }
145
146 // 
147