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         
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             echo "Pman.{$mn} = Pman.{$mn} || {};\n";
69             echo "Pman.{$mn}.template = Pman.{$mn}.template   || {};\n\n";
70             
71             foreach(glob("$dir/*.html") as $fn) {
72                 $name = 'Pman.' . $mn .'.template.' . preg_replace('/\.html$/i', '', basename($fn));
73                 echo $this->compile($fn, $name) . "\m";
74                 
75
76             }
77   //              testing..
78 //new HTML_FlexyFramework_JsTemplate('/home/alan/gitlive/web.mtrack/MTrackWeb/jtemplates/TimelineTicket.html', 'Pman.template.TimelineTicket');
79             
80             
81             
82         }
83         exit;
84         
85         
86         
87     }
88     
89     
90     function compile($fn, $name)
91     {
92         // cached? - check file see if we have cached contents.
93         
94         
95         $contents = file_get_contents($fn);
96         $ar = preg_split('/(\{[^\}]+})/', $contents, -1, PREG_SPLIT_DELIM_CAPTURE);
97         //echo '<PRE>' . htmlspecialchars(print_r($ar,true));
98         
99         $ret = array();
100         
101         $ret[] = "var $name = function(t) {\n    var ret=[];\n";
102         $indent = 1;
103         foreach($ar as $item) {
104             $in = str_repeat("    ", $indent);
105             
106             //var_Dump(substr($item,-3,2));
107             switch(true) {
108                 case (!strlen($item)):
109                     continue;
110                 
111                 case ($item[0] != '{'):
112                     if (!strlen(trim($item))) {
113                         continue;
114                     }
115                     $ret[] = $in . "ret+= ". json_encode($item) . ";";
116                     continue;
117                 
118                 case (substr($item,1,3) == 'if('):
119                     $ret[] = $in . substr($item,1,-1) . ' {';
120                     $indent++;
121                     continue;
122                 
123                 case (substr($item,1,4) == 'end:'):
124                     $indent--;
125                     $in = str_repeat("    ", $indent);
126                     $ret[] = $in . "}";
127                     continue;
128                 
129                 case (substr($item,1,7) == 'return:'):
130                     $ret[] = $in . "return;";
131                     continue;
132                 
133                 case (substr($item,1,9) == 'function:'):
134                     $indent++;
135                     $def  = substr($item,10,-1) ;
136                     list($name,$body) = explode('(', $def, 2);
137                     
138                     
139                     $ret[] = $in . "var $name = function (" .  $body  . '{';
140                     continue;
141                 
142                 default:
143                     if (substr($item,-3,2) == ':h') {
144                         $ret[] = $in . "ret += ".  substr($item,1,-3) . ';';
145                         continue;
146                     }
147                     $ret[] = $in . "ret += Roo.util.Format.htmlEncode(".  substr($item,1,-1).');';
148                     continue;
149                 
150             }
151             
152             
153         }
154         $in = str_repeat("    ", $indent);
155         $ret[] = $in .  "return ret.join('');\n}\n";
156         return implode("\n",$ret);
157         echo '<PRE>' . htmlspecialchars(implode("\n",$ret));
158         
159         
160         
161     }
162     
163     
164     
165 }
166
167 // 
168