From 137e602fe71b52c7972e65f80221ca61062c4877 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Sat, 17 Feb 2007 23:30:00 +0000 Subject: [PATCH] Added an additional parsing step in which curly braces are parsed into PHP code no matter what, so that way we can use all kinds of cool functions in them --- Template.php | 98 +++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 82 insertions(+), 16 deletions(-) diff --git a/Template.php b/Template.php index dfa87be..3f629ac 100644 --- a/Template.php +++ b/Template.php @@ -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 -- 2.22.5