From 5ca97c7099d805170a0498e59f83647804807f4e Mon Sep 17 00:00:00 2001 From: Hamish Friedlander Date: Fri, 25 Mar 2011 12:28:03 +1300 Subject: [PATCH] BUGFIX: Make SSTemplateParser work with empty templates --- core/SSTemplateParser.php | 31 ++++++++++++++++++------------- core/SSTemplateParser.php.inc | 31 ++++++++++++++++++------------- 2 files changed, 36 insertions(+), 26 deletions(-) diff --git a/core/SSTemplateParser.php b/core/SSTemplateParser.php index 441e251d5..29d85fb80 100644 --- a/core/SSTemplateParser.php +++ b/core/SSTemplateParser.php @@ -3904,19 +3904,24 @@ class SSTemplateParser extends Parser { * @return mixed|string - The php that, when executed (via include or exec) will behave as per the template source */ static function compileString($string, $templateName = "", $includeDebuggingComments=false) { - // Construct a parser instance - $parser = new SSTemplateParser($string); - $parser->includeDebuggingComments = $includeDebuggingComments; - - // Ignore UTF8 BOM at begining of string. TODO: Confirm this is needed, make sure SSViewer handles UTF (and other encodings) properly - if(substr($string, 0,3) == pack("CCC", 0xef, 0xbb, 0xbf)) $parser->pos = 3; - - // Match the source against the parser - $result = $parser->match_TopTemplate(); - if(!$result) throw new SSTemplateParseException('Unexpected problem parsing template', $parser); - - // Get the result - $code = $result['php']; + if (!trim($string)) { + $code = ''; + } + else { + // Construct a parser instance + $parser = new SSTemplateParser($string); + $parser->includeDebuggingComments = $includeDebuggingComments; + + // Ignore UTF8 BOM at begining of string. TODO: Confirm this is needed, make sure SSViewer handles UTF (and other encodings) properly + if(substr($string, 0,3) == pack("CCC", 0xef, 0xbb, 0xbf)) $parser->pos = 3; + + // Match the source against the parser + $result = $parser->match_TopTemplate(); + if(!$result) throw new SSTemplateParseException('Unexpected problem parsing template', $parser); + + // Get the result + $code = $result['php']; + } // Include top level debugging comments if desired if($includeDebuggingComments && $templateName && stripos($code, "includeDebuggingComments = $includeDebuggingComments; - - // Ignore UTF8 BOM at begining of string. TODO: Confirm this is needed, make sure SSViewer handles UTF (and other encodings) properly - if(substr($string, 0,3) == pack("CCC", 0xef, 0xbb, 0xbf)) $parser->pos = 3; - - // Match the source against the parser - $result = $parser->match_TopTemplate(); - if(!$result) throw new SSTemplateParseException('Unexpected problem parsing template', $parser); - - // Get the result - $code = $result['php']; + if (!trim($string)) { + $code = ''; + } + else { + // Construct a parser instance + $parser = new SSTemplateParser($string); + $parser->includeDebuggingComments = $includeDebuggingComments; + + // Ignore UTF8 BOM at begining of string. TODO: Confirm this is needed, make sure SSViewer handles UTF (and other encodings) properly + if(substr($string, 0,3) == pack("CCC", 0xef, 0xbb, 0xbf)) $parser->pos = 3; + + // Match the source against the parser + $result = $parser->match_TopTemplate(); + if(!$result) throw new SSTemplateParseException('Unexpected problem parsing template', $parser); + + // Get the result + $code = $result['php']; + } // Include top level debugging comments if desired if($includeDebuggingComments && $templateName && stripos($code, "