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();
49         
50         $pr = $ff->project;
51         $ar = explode(PATH_SEPARATOR, $ff->HTML_Template_Flexy['templateDir']);
52         
53         $prefix = $pr == 'Pman' ? 'Pman.' : '';
54         
55         foreach($ar as $mod) {
56             $dir =   dirname($mod) . '/jtemplates';
57             if (!file_exists($dir)) {
58                 echo '// missing directory '. htmlspecialchars($dir) ."\n";
59                 continue;
60             }
61             // got a directory..
62             $mn = basename(dirname($mod));
63             $ar = glob("$dir/*.html") ;
64             if (empty($ar)) {
65                 echo '// no template is directory '. htmlspecialchars($dir) ."\n";
66                 continue;
67             }
68             
69             echo "{$prefix}{$mn} = {$prefix}{$mn} || {};\n";
70             echo "{$prefix}{$mn}.template = {$prefix}{$mn}.template   || {};\n\n";
71             
72             foreach(glob("$dir/*.html") as $fn) {
73                 $name = "{$prefix}{$mn}.template." . preg_replace('/\.html$/i', '', basename($fn));
74                 echo $this->compile($fn, $name) . "\n";
75                 
76
77             }
78   //              testing..
79 //new HTML_FlexyFramework_JsTemplate('/home/alan/gitlive/web.mtrack/MTrackWeb/jtemplates/TimelineTicket.html', 'Pman.template.TimelineTicket');
80             
81             
82             
83         }
84         exit;
85         
86         
87         
88     }
89     
90     
91     function compile($fn, $name)
92     {
93         // cached? - check file see if we have cached contents.
94         
95         
96         $contents = file_get_contents($fn);
97         $ar = preg_split('/(\{[^\\n}]+})/', $contents, -1, PREG_SPLIT_DELIM_CAPTURE);
98         
99         
100         
101         
102         //echo '<PRE>' . htmlspecialchars(print_r($ar,true));
103         
104         $out= array();
105         
106         $head = "$name = function(t)\n{\n    var ret = [];\n\n";
107         
108         $funcs = array();
109         // do not allow nested functions..?
110         $fstart = -1;
111         $indent = 2;
112         $inscript = false;
113         $ret = &$out;
114         foreach($ar as $item) {
115             $in = str_repeat("    ", $indent);
116             $indent  = max($indent , 1);
117             //var_Dump(substr($item,-3,2));
118             switch(true) {
119                 case (!strlen($item)):
120                     continue;
121                 
122                 case ($inscript && ($item != '{end:}')):
123                     $ret[] = $item;
124                     continue;
125                 
126                 case ($inscript && ($item == '{end:}')):
127                     $inscript = false;
128                     continue;
129                  
130              
131                 case ($item[0] != '{'):
132                     if (!strlen(trim($item))) {
133                         continue;
134                     }
135                     $ret[] = $in . "ret += ". json_encode($item) . ";";
136                     continue;
137                 
138                 
139                 case ($item == '{script:}'): 
140                     $inscript = true;
141                     continue;
142                 
143                 case ($item[1] == '!'):
144                     $ret[] = $in . substr($item,2,-1) .';';
145                     continue;
146                 
147                 
148                 case (substr($item,1,3) == 'if('):
149                     $ret[] = $in . substr($item,1,-1) . ' {';
150                     $indent++;
151                     continue;
152                 
153                 case (substr($item,1,4) == 'end:'):
154                     $indent--;
155                     $in = str_repeat("    ", $indent);
156                     $ret[] = $in . "}";
157                     if ($fstart == $indent) {
158                         $fstart = -1;
159                         $ret = &$out;
160                     }
161                     continue;
162                 
163                 case (substr($item,1,7) == 'return:'):
164                     $ret[] = $in . "return;";
165                     continue;
166                 
167                 case (substr($item,1,9) == 'function:'):
168                     $fstart = $indent;
169                     $indent++;
170                     $ret = &$funcs;
171                     $def  = substr($item,10,-1) ;
172                     list($name,$body) = explode('(', $def, 2);
173                     
174                     
175                     $ret[] = $in . "var $name = function (" .  $body  . '{';
176                     continue;
177                 
178                 default:
179                     if (substr($item,-3,2) == ':h') {
180                         $ret[] = $in . "ret += ".  substr($item,1,-3) . ';';
181                         continue;
182                     }
183                     if (substr($item,-3,2) == ':b') {
184                         $ret[] = $in . "ret += Roo.util.Format.htmlEncode(".  substr($item,1,-3).').split("\n").join("<br/>\n");';
185                         continue;
186                     }
187                     $ret[] = $in . "ret += Roo.util.Format.htmlEncode(".  substr($item,1,-1).');';
188                     continue;
189                 
190             }
191             
192             
193         }
194         $in = str_repeat("    ", $indent);
195         $ret[] = $in .  "return ret.join('');\n}\n";
196         return $head . implode("\n",$funcs) . "\n\n" .implode("\n",$out) ;
197         //echo '<PRE>' . htmlspecialchars(implode("\n",$ret));
198         
199         
200         
201     }
202     
203     
204     
205 }
206
207 // 
208