/** * Test calls to <% require %> methods */ public function testRequireCalls() { // String arg $template = '<% require javascript("path/to/some.js") %>'; $expected = <<<'PHP' Requirements::javascript('path/to/some.js'); PHP; $this->assertContains($expected, $this->parser->compileString($template)); // Lookup arg $template = '<% require javascript($Foo) %>'; $expected = <<<'PHP' Requirements::javascript($scope->locally()->XML_val('Foo', null, true)); PHP; $this->assertContains($expected, $this->parser->compileString($template)); // Concatenation $template = '<% require javascript(SOME_CONST . "js/file.js") %>'; $expected = <<<'PHP' Requirements::javascript(SOME_CONST . "js/file.js"); PHP; $this->assertContains($expected, $this->parser->compileString($template)); }
/** * Compiles some passed template source code into the php code that will execute as per the template source. * * @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 * @param bool $topTemplate True if this is a top template, false if it's just a template * @return mixed|string The php that, when executed (via include or exec) will behave as per the template source * @todo tidy */ public function compileString($string, $templateName = '', $includeDebuggingComments = false, $topTemplate = true) { if (!trim($string)) { $code = ''; } else { parent::__construct($string); $this->includeDebuggingComments = $includeDebuggingComments; // Match the source against the parser if ($topTemplate) { $result = $this->match_TopTemplate(); } else { $result = $this->match_Template(); } if (!$result) { throw new SSTemplateParseException('Unexpected problem parsing template', $this); } // Get the result $code = $result['php']; } // Include top level debugging comments if desired if ($includeDebuggingComments && $templateName && stripos($code, "<?xml") === false) { $code = $this->includeDebuggingComments($code, $templateName); } return $code; }