public static function parseTemplateContent($content, $template = "") { return SSTemplateParser::compileString($content, $template, Director::isDev() && self::$source_file_comments); }
/** * Compiles some passed template source code into the php code that will execute as per the template source. * * @static * @throws SSTemplateParseException * @param $string - The source of the template * @param string $templateName - The name of the template, normally the filename the template source was loaded from * @param bool $includeDebuggingComments - True is debugging comments should be included in the output * @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) { 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, "<?xml") === false) { // If this template is a full HTML page, then put the comments just inside the HTML tag to prevent any IE glitches if(stripos($code, "<html") !== false) { $code = preg_replace('/(<html[^>]*>)/i', "\\1<!-- template $templateName -->", $code); $code = preg_replace('/(<\/html[^>]*>)/i', "<!-- end template $templateName -->\\1", $code); } else { $code = str_replace('<?php' . PHP_EOL, '<?php' . PHP_EOL . '$val .= \'<!-- template ' . $templateName . ' -->\';' . "\n", $code); $code .= "\n" . '$val .= \'<!-- end template ' . $templateName . ' -->\';'; } } return $code; }
public function testOpenBlockExtension() { $count = 0; $parser = new SSTemplateParser(); $parser->addOpenBlock('test', function (&$res) use(&$count) { $count++; }); $template = new SSViewer_FromString("<% test %>", $parser); $template->process(new SSViewerTestFixture()); $this->assertEquals(1, $count); }