X-Git-Url: http://git.roojs.org/?p=Pman.Core;a=blobdiff_plain;f=JsTemplate.php;h=ec79d0c46fb0644ceb5325a04a2e02c1e6b234da;hp=aa124f54d3e189728940de78f4e612582c8c9c29;hb=HEAD;hpb=82a10ef6c93bea97df22efefd3c19e7ce032ef36 diff --git a/JsTemplate.php b/JsTemplate.php index aa124f54..302b1c6d 100644 --- a/JsTemplate.php +++ b/JsTemplate.php @@ -40,37 +40,47 @@ class Pman_Core_JsTemplate extends Pman { } - function get() + function get($v, $opts=array()) { + + $this->sessionState(0); // get the modules. header('Content-type: text/javascript'); $ff = HTML_FlexyFramework::get(); $pr = $ff->project; - $ar = explode(PATH_SEPARATOR, $ff->HTML_Template_Flexy['templateDir']); - $prefix = $pr == 'Pman' ? 'Pman.' : ''; + $mods = $this->modulesList(); + //print_r($mods); + + //$ar = explode(PATH_SEPARATOR, $ff->HTML_Template_Flexy['templateDir']); + array_push($mods, $pr); - foreach($ar as $mod) { - $dir = dirname($mod) . '/jtemplates'; + foreach($mods as $mod ) + { + $prefix = $mod == $pr ? "" : "{$pr}."; + //var_dump($prefix); + $pdir = $mod == $pr ? '' : ($pr .'/') ; + + $dir = $this->rootDir .'/'.$pdir . $mod . '/jtemplates'; if (!file_exists($dir)) { - echo '// missing directory '. htmlspecialchars($dir) ."\n"; + //echo '// missing directory '. htmlspecialchars($dir) ."\n"; continue; } // got a directory.. - $mn = basename(dirname($mod)); + $ar = glob("$dir/*.html") ; if (empty($ar)) { echo '// no template is directory '. htmlspecialchars($dir) ."\n"; continue; } - echo "{$prefix}{$mn} = {$prefix}{$mn} || {};\n"; - echo "{$prefix}{$mn}.template = {$prefix}{$mn}.template || {};\n\n"; + echo "{$prefix}{$mod} = {$prefix}{$mod} || {};\n"; + echo "{$prefix}{$mod}.template = {$prefix}{$mod}.template || {};\n\n"; foreach(glob("$dir/*.html") as $fn) { - $name = "{$prefix}{$mn}.template." . preg_replace('/\.html$/i', '', basename($fn)); + $name = "{$prefix}{$mod}.template." . preg_replace('/\.html$/i', '', basename($fn)); echo $this->compile($fn, $name) . "\n"; @@ -94,71 +104,115 @@ class Pman_Core_JsTemplate extends Pman { $contents = file_get_contents($fn); - $ar = preg_split('/(\{[^\}]+})/', $contents, -1, PREG_SPLIT_DELIM_CAPTURE); - //echo '
' . htmlspecialchars(print_r($ar,true));
+        $ar = preg_split('/(\{[^\\n}]+})/', $contents, -1, PREG_SPLIT_DELIM_CAPTURE);
+        
         
-        $ret = array();
         
-        $ret[] = "var $name = function(t) {\n    var ret=[];\n";
         
-        $funcs = [];
+        //echo '
' . htmlspecialchars(print_r($ar,true));
+        
+        $out= array();
+        
+        $head = "$name = function(t)\n{\n    var ret = '';\n\n";
         
-        $indent = 1;
+        $funcs = array();
+        // do not allow nested functions..?
+        $fstart = -1;
+        $indent = 2;
+        $inscript = false;
+        $ret = &$out;
         foreach($ar as $item) {
             $in = str_repeat("    ", $indent);
-            
+            $indent  = max($indent , 1);
             //var_Dump(substr($item,-3,2));
             switch(true) {
                 case (!strlen($item)):
-                    continue;
+                    continue 2;
+                
+                case ($inscript && ($item != '{end:}')):
+                    $ret[count($ret)-1] .= $item;
+                    continue 2;
                 
+                case ($inscript && ($item == '{end:}')):
+                    $inscript = false;
+                    continue 2;
+                 
+             
                 case ($item[0] != '{'):
                     if (!strlen(trim($item))) {
-                        continue;
+                        continue 2;
                     }
                     $ret[] = $in . "ret += ". json_encode($item) . ";";
-                    continue;
+                    continue 2;
+                
+                
+                case ($item == '{script:}'): 
+                    $inscript = true;
+                     $ret[] = '';
+                    continue 2;
+                
+                case ($item[1] == '!'):
+                    $ret[] = $in . substr($item,2,-1) .';';
+                    continue 2;
+                
                 
                 case (substr($item,1,3) == 'if('):
                     $ret[] = $in . substr($item,1,-1) . ' {';
                     $indent++;
-                    continue;
+                    continue 2;
                 
+                case (substr($item,1,5) == 'else:'):
+                    $indent--;
+                    $in = str_repeat("    ", $indent);
+                    $ret[] = $in . "} else { ";
+                    $indent++;
+                    continue 2;
+                 
                 case (substr($item,1,4) == 'end:'):
                     $indent--;
                     $in = str_repeat("    ", $indent);
                     $ret[] = $in . "}";
-                    continue;
+                    if ($fstart == $indent) {
+                        $fstart = -1;
+                        $ret = &$out;
+                    }
+                    continue 2;
                 
                 case (substr($item,1,7) == 'return:'):
                     $ret[] = $in . "return;";
-                    continue;
+                    continue 2;
                 
                 case (substr($item,1,9) == 'function:'):
+                    $fstart = $indent;
                     $indent++;
+                    $ret = &$funcs;
                     $def  = substr($item,10,-1) ;
                     list($name,$body) = explode('(', $def, 2);
                     
                     
                     $ret[] = $in . "var $name = function (" .  $body  . '{';
-                    continue;
+                    continue 2;
                 
                 default:
                     if (substr($item,-3,2) == ':h') {
                         $ret[] = $in . "ret += ".  substr($item,1,-3) . ';';
-                        continue;
+                        continue 2;
+                    }
+                    if (substr($item,-3,2) == ':b') {
+                        $ret[] = $in . "ret += Roo.util.Format.htmlEncode(".  substr($item,1,-3).').split("\n").join("
\n");'; + continue 2; } $ret[] = $in . "ret += Roo.util.Format.htmlEncode(". substr($item,1,-1).');'; - continue; + continue 2; } } $in = str_repeat(" ", $indent); - $ret[] = $in . "return ret.join('');\n}\n"; - return implode("\n",$ret); - echo '
' . htmlspecialchars(implode("\n",$ret));
+        $ret[] = $in .  "return ret;\n}\n";
+        return $head . implode("\n",$funcs) . "\n\n" .implode("\n",$out) ;
+        //echo '
' . htmlspecialchars(implode("\n",$ret));