JsTemplate.php
[Pman.Core] / JsTemplate.php
index b720d0e..f05a44a 100644 (file)
@@ -43,17 +43,22 @@ class Pman_Core_JsTemplate extends Pman {
     function get()
     {
         // get the modules.
-        header('Content-type: text/javascript');
+        //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);
         
-        foreach($ar as $mod) {
-            $dir =   dirname($mod) . '/jtemplates';
+        //$ar = explode(PATH_SEPARATOR, $ff->HTML_Template_Flexy['templateDir']);
+        
+        $prefix = empty($mods) ? '' : ($pr .'.') ;
+        $pdir = empty($mods) ? '' : ($pr .'/') ;
+
+        foreach(empty($mods) ? array($pr) : $mods as $mod ) {
+            $dir =  $this->rootDir .'/'.$pdir .  $mod . '/jtemplates';
             if (!file_exists($dir)) {
                 echo '// missing directory '. htmlspecialchars($dir) ."\n";
                 continue;
@@ -94,7 +99,7 @@ class Pman_Core_JsTemplate extends Pman {
         
         
         $contents = file_get_contents($fn);
-        $ar = preg_split('/(\{[^\}]+})/', $contents, -1, PREG_SPLIT_DELIM_CAPTURE);
+        $ar = preg_split('/(\{[^\\n}]+})/', $contents, -1, PREG_SPLIT_DELIM_CAPTURE);
         
         
         
@@ -103,29 +108,31 @@ class Pman_Core_JsTemplate extends Pman {
         
         $out= array();
         
-        $head = "$name = function(t) {\n    var ret=[];\n";
+        $head = "$name = function(t)\n{\n    var ret = [];\n\n";
         
         $funcs = array();
         // do not allow nested functions..?
         $fstart = -1;
-        $indent = 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;
                 
-                if ($inscript && ($item != '{end:}')) {
-                    $ret[] = $item;
-                }
-                if ($inscript && ($item == '{end:}')) {
+                case ($inscript && ($item != '{end:}')):
+                    $ret[count($ret)-1] .= $item;
+                    continue;
+                
+                case ($inscript && ($item == '{end:}')):
                     $inscript = false;
-                    
-                }
+                    continue;
+                 
+             
                 case ($item[0] != '{'):
                     if (!strlen(trim($item))) {
                         continue;
@@ -134,10 +141,14 @@ class Pman_Core_JsTemplate extends Pman {
                     continue;
                 
                 
-                case (substr($item,1,6) == 'script:'): 
+                case ($item == '{script:}'): 
                     $inscript = true;
+                     $ret[] = '';
                     continue;
                 
+                case ($item[1] == '!'):
+                    $ret[] = $in . substr($item,2,-1) .';';
+                    continue;
                 
                 
                 case (substr($item,1,3) == 'if('):
@@ -145,6 +156,13 @@ class Pman_Core_JsTemplate extends Pman {
                     $indent++;
                     continue;
                 
+                case (substr($item,1,5) == 'else:'):
+                    $indent--;
+                    $in = str_repeat("    ", $indent);
+                    $ret[] = $in . "} else { ";
+                    $indent++;
+                    continue;
+                 
                 case (substr($item,1,4) == 'end:'):
                     $indent--;
                     $in = str_repeat("    ", $indent);
@@ -175,6 +193,10 @@ class Pman_Core_JsTemplate extends Pman {
                         $ret[] = $in . "ret += ".  substr($item,1,-3) . ';';
                         continue;
                     }
+                    if (substr($item,-3,2) == ':b') {
+                        $ret[] = $in . "ret += Roo.util.Format.htmlEncode(".  substr($item,1,-3).').split("\n").join("<br/>\n");';
+                        continue;
+                    }
                     $ret[] = $in . "ret += Roo.util.Format.htmlEncode(".  substr($item,1,-1).');';
                     continue;
                 
@@ -183,8 +205,8 @@ class Pman_Core_JsTemplate extends Pman {
             
         }
         $in = str_repeat("    ", $indent);
-        $ret[] = $in .  "return ret.join('');\n}\n";
-        return $head . implode("\n",$funcs) . "\n" .implode("\n",$out) ;
+        $ret[] = $in .  "return ret;\n}\n";
+        return $head . implode("\n",$funcs) . "\n\n" .implode("\n",$out) ;
         //echo '<PRE>' . htmlspecialchars(implode("\n",$ret));