Added an additional parsing step in which curly braces are parsed into PHP code no...
authorRobert Sesek <rsesek@bluestatic.org>
Sat, 17 Feb 2007 23:30:00 +0000 (23:30 +0000)
committerRobert Sesek <rsesek@bluestatic.org>
Sat, 17 Feb 2007 23:30:00 +0000 (23:30 +0000)
Template.php

index dfa87be7bfbfa91b0927f70371d4864c0286ebde..3f629ac0f7241495b1ded99d185b59e8516993a5 100644 (file)
@@ -335,11 +335,93 @@ class BSTemplate
                        $template = call_user_func($this->preParseHook, $template);
                }
                
+               $template = $this->_parseBlocksAndTokens($template);
                $template = $this->_parsePhrases($template);
                $template = $this->_parseConditionals($template);
                return $template;
        }
        
+       // ###################################################################
+       /**
+       * Parses anything with curly braces {} (including phrases)
+       *
+       * @param        string  Template data
+       *
+       * @return       string  Parsed template data
+       */
+       private function _parseBlocksAndTokens($template)
+       {
+               $stack = array();
+               $tokens = array();
+               
+               while (1)
+               {
+                       for ($i = 0; $i < strlen($template); $i++)
+                       {
+                               // we've run through the template and there's nothing in the stack--done
+                               if ($i == strlen($template) - 1 AND sizeof($stack) == 0)
+                               {
+                                       return $template;
+                               }
+                               
+                               if ($template[$i] == '{')
+                               {
+                                       // ignore escaped sequences
+                                       if ($template[$i - 1] != '\\')
+                                       {
+                                               array_push($stack, $i);
+                                       }
+                               }
+                               else if ($template[$i] == '}')
+                               {
+                                       // there's no stack so it was probably escaped
+                                       if (sizeof($stack) == 0)
+                                       {
+                                               continue;
+                                       }
+                                       // we're good and nested
+                                       else if (sizeof($stack) == 1)
+                                       {
+                                               $open = array_pop($stack);
+                                               $token = substr($template, $open, $i - $open + 1);
+                                               $template = str_replace($token, $this->_parseToken($token), $template);
+                                               break;
+                                       }
+                                       // just pop it off
+                                       else
+                                       {
+                                               array_pop($stack);
+                                       }
+                               }
+                       }
+               }
+       }
+       
+       // ###################################################################
+       /**
+       * Parses a curly brace token {}
+       *
+       * @param        string  Token
+       *
+       * @return       string  Parsed value
+       */
+       private function _parseToken($token)
+       {
+               // knock of the braces
+               $token = substr($token, 1, strlen($token) - 2);
+               
+               // language token
+               if ($token[0] == '@' AND $token[1] == '\\' AND $token[2] == '"')
+               {
+                       return '" . ' . $this->langcall . '(\'' . str_replace(array('\\\"', "'"), array('"', "\'"), substr($token, 3, strlen($token) - 5)) . '\')' . ' . "';
+               }
+               // normal PHP code
+               else
+               {
+                       return '" . (' . $token . ') . "';
+               }
+       }
+       
        // ###################################################################
        /**
        * Prepares language and locale information inside templates
@@ -413,25 +495,9 @@ class BSTemplate
                        unset($arglist);
                }
                
-               // Process the empty phrase objects -- do this now so we don't have to worry about it when we're parsing later
-               $template = preg_replace('#\{@\\\"(.*?)\\\"\}#ise', '$this->_templateString(\'$1\')', $template);
-               
                return $template;
        }
        
-       // ###################################################################
-       /**
-       * Turns a localized phrase tag into a function call
-       *
-       * @param        string  Phrase text
-       *
-       * @return       string  Function call for phrase text
-       */
-       private function _templateString($text)
-       {
-               return '" . ' . $this->langcall . '(\'' . str_replace(array('\\\"', "'"), array('"', "\'"), $text) . '\') . "';
-       }
-       
        // ###################################################################
        /**
        * Parser for in-line template conditionals