/** * @param string $code * @return string * @throws \Athem\Exception\InvalidClass */ protected function phpProcess($code, $data = array()) { if (!class_exists('\\MtHaml\\Environment')) { throw new Exception\InvalidClass('Could nod find \\MtHaml\\Environment class. If you\'re using ' . 'composer, please add "mthaml/mthaml" : "*" to your composer.json and run \'composer update\'.'); } try { // change error handler $oldErrorHandler = set_error_handler(array($this, 'hamlRenderingErrorHandler')); $haml = new \MtHaml\Environment('php'); $executor = new \MtHaml\Support\Php\Executor($haml, $this->options['phpHamlOptions']); /* $haml = new \MtHaml\Environment('php'); $haml-> $code = $haml->compileString($code, ''); $tmpName = 'Athem_Code_Html_Compile_MtHaml_' . Stdlib\Utils\StringUtils::randString(50); $tmpFile = $this->options['hamlOptions']['cache'] . DIRECTORY_SEPARATOR . $tmpName . '.php'; $tmpCode = <<<PHP <?php function $tmpName(\$__variables) { extract(\$__variables); ?>$code<?php } PHP; file_put_contents($tmpFile, $tmpCode); $this->phpSyntaxCheck($tmpFile);*/ $level = ob_get_level(); ob_start(); /* require_once $tmpFile; try { call_user_func_array($tmpName, array($data)); } catch (\Exception $e) { while (ob_get_level() > $level) { ob_end_clean(); } throw $e; }*/ $executor->display($code, $data); $rendered = ob_get_clean(); // unlink($tmpFile); set_error_handler($oldErrorHandler); // return rendered code return trim($rendered); } catch (\Exception $e) { throw new Code\Exception\CompileError('Code processing failed. Please see trace to understand the nature of the error.', 1, $e); } }
public function phpProcess($code, $data = array()) { /* $hamlFile = '/tmp/' . \Athem\Stdlib\Utils\StringUtils::randString(50) . '.haml'; (new \Athem\Stdlib\FileObject($hamlFile, 'w+'))->write($code); $haml = new \MtHaml\Environment('php'); $executor = new \MtHaml\Support\Php\Executor($haml, array('cache' => '/tmp')); $rendered = $executor->render($hamlFile, $data); unlink($hamlFile); return trim($rendered);*/ ob_start(); $haml = new \MtHaml\Environment('php'); $executor = new \MtHaml\Support\Php\Executor($haml, array('cache' => '/tmp')); $executor->display($code, $this->data); return ob_get_clean(); }
<?php /** * This example shows how to integrate MtHaml with PHP templates. */ require __DIR__ . "/autoload.php"; $haml = new MtHaml\Environment('php'); $hamlExecutor = new MtHaml\Support\Php\Executor($haml, array('cache' => sys_get_temp_dir() . '/haml')); /* * Execute the template */ echo "\n\nExecuted Template:\n\n"; $template = __DIR__ . '/example-php.haml'; $variables = array('foo' => 'bar'); try { $hamlExecutor->display($template, $variables); } catch (MtHaml\Exception $e) { echo "Failed to execute template: ", $e->getMessage(), "\n"; } /* * See how it was compiled */ echo "\n\nHow the template was compiled:\n\n"; echo $haml->compileString(file_get_contents($template), $template), "\n";