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