6 * just need to add this to HTML
8 * <script src="{baseURL}/Core/JsTemplate.js">
14 * first part should return a list of files to include.
15 * $x = new Pman_Core_JsTemplate($cfg)
19 * second part should compile and deliver.
23 * // should return {baseurl}/Pman/JsTemplate/mod/file
29 require_once 'Pman.php';
31 class Pman_Core_JsTemplate extends Pman {
46 header('Content-type: text/javascript');
48 $ff = HTML_FlexyFramework::get();
52 $mods = $this->modulesList();
55 //$ar = explode(PATH_SEPARATOR, $ff->HTML_Template_Flexy['templateDir']);
56 array_push($mods, $pr);
58 foreach($mods as $mod )
60 $prefix = $mod == $pr ? "" : "{$pr}.";
62 $pdir = $mod == $pr ? '' : ($pr .'/') ;
64 $dir = $this->rootDir .'/'.$pdir . $mod . '/jtemplates';
65 if (!file_exists($dir)) {
66 echo '// missing directory '. htmlspecialchars($dir) ."\n";
71 $ar = glob("$dir/*.html") ;
73 echo '// no template is directory '. htmlspecialchars($dir) ."\n";
77 echo "{$prefix}{$mod} = {$prefix}{$mod} || {};\n";
78 echo "{$prefix}{$mod}.template = {$prefix}{$mod}.template || {};\n\n";
80 foreach(glob("$dir/*.html") as $fn) {
81 $name = "{$prefix}{$mod}.template." . preg_replace('/\.html$/i', '', basename($fn));
82 echo $this->compile($fn, $name) . "\n";
87 //new HTML_FlexyFramework_JsTemplate('/home/alan/gitlive/web.mtrack/MTrackWeb/jtemplates/TimelineTicket.html', 'Pman.template.TimelineTicket');
99 function compile($fn, $name)
101 // cached? - check file see if we have cached contents.
104 $contents = file_get_contents($fn);
105 $ar = preg_split('/(\{[^\\n}]+})/', $contents, -1, PREG_SPLIT_DELIM_CAPTURE);
110 //echo '<PRE>' . htmlspecialchars(print_r($ar,true));
114 $head = "$name = function(t)\n{\n var ret = '';\n\n";
117 // do not allow nested functions..?
122 foreach($ar as $item) {
123 $in = str_repeat(" ", $indent);
124 $indent = max($indent , 1);
125 //var_Dump(substr($item,-3,2));
127 case (!strlen($item)):
130 case ($inscript && ($item != '{end:}')):
131 $ret[count($ret)-1] .= $item;
134 case ($inscript && ($item == '{end:}')):
139 case ($item[0] != '{'):
140 if (!strlen(trim($item))) {
143 $ret[] = $in . "ret += ". json_encode($item) . ";";
147 case ($item == '{script:}'):
152 case ($item[1] == '!'):
153 $ret[] = $in . substr($item,2,-1) .';';
157 case (substr($item,1,3) == 'if('):
158 $ret[] = $in . substr($item,1,-1) . ' {';
162 case (substr($item,1,5) == 'else:'):
164 $in = str_repeat(" ", $indent);
165 $ret[] = $in . "} else { ";
169 case (substr($item,1,4) == 'end:'):
171 $in = str_repeat(" ", $indent);
173 if ($fstart == $indent) {
179 case (substr($item,1,7) == 'return:'):
180 $ret[] = $in . "return;";
183 case (substr($item,1,9) == 'function:'):
187 $def = substr($item,10,-1) ;
188 list($name,$body) = explode('(', $def, 2);
191 $ret[] = $in . "var $name = function (" . $body . '{';
195 if (substr($item,-3,2) == ':h') {
196 $ret[] = $in . "ret += ". substr($item,1,-3) . ';';
199 if (substr($item,-3,2) == ':b') {
200 $ret[] = $in . "ret += Roo.util.Format.htmlEncode(". substr($item,1,-3).').split("\n").join("<br/>\n");';
203 $ret[] = $in . "ret += Roo.util.Format.htmlEncode(". substr($item,1,-1).');';
210 $in = str_repeat(" ", $indent);
211 $ret[] = $in . "return ret;\n}\n";
212 return $head . implode("\n",$funcs) . "\n\n" .implode("\n",$out) ;
213 //echo '<PRE>' . htmlspecialchars(implode("\n",$ret));