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