/** * @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(); }
function render($templateName, $variables = [], $inLayout = true) { $variables["DOCUMENT_ROOT"] = $_SERVER['DOCUMENT_ROOT']; $variables["controller"] = $this; if ($templateName[0] == "/") { $templatePath = $_SERVER['DOCUMENT_ROOT'] . "/app/views" . $templateName; } else { $templatePath = $_SERVER['DOCUMENT_ROOT'] . "/app/views/" . $this->controller . "/" . $templateName; } if ($this->format == "html") { $haml = new MtHaml\Environment('php', array('enable_escaper' => false)); $hamlExecutor = new MtHaml\Support\Php\Executor($haml, array('cache' => $_SERVER['DOCUMENT_ROOT'] . "/tmp/haml")); try { $content = $hamlExecutor->render($templatePath, $variables); } catch (MtHaml\Exception $e) { return "Failed to execute template: " . $templateName . " " . $e->getMessage() . "\n"; } if (!$inLayout) { return $content; } $layoutPath = $_SERVER['DOCUMENT_ROOT'] . "/app/views/layouts/" . $this->layout; $variables["layoutContent"] = $content; try { $response = $hamlExecutor->render($layoutPath, $variables); } catch (MtHaml\Exception $e) { return "Failed to execute layout " . $this->layout . ": " . $e->getMessage() . "\n"; } return $response; } else { if ($format == "json") { return "JSON FORMAT NOT IMPLEMENTED YET"; } else { return "UNKNOWN FORMAT " . $this->format; } } }
#!/usr/bin/php <?php $config = ['git_urls' => ['https://github.com/arnaud-lb/MtHaml.git' => 'mthaml/'], 'autoload_config' => ['mthaml/lib/MtHaml/' => 'MtHaml'], 'example' => function () { $haml = new MtHaml\Environment('php'); $executor = new MtHaml\Support\Php\Executor($haml, ['cache' => sys_get_temp_dir() . '/haml']); $tpl = ' %ul#users %li.user '; $path = sys_get_temp_dir() . 'sample.haml'; file_put_contents($path, $tpl); echo $executor->render($path, ['var' => 'value']); }]; if ($return_config) { return $config; } require_once __DIR__ . '/_yf_autoloader.php'; new yf_autoloader($config);
/** * Process and output HAML content */ function haml($content, $params = []) { $this->require_php_lib('mthaml'); $haml = new MtHaml\Environment('php'); $executor = new MtHaml\Support\Php\Executor($haml, ['cache' => sys_get_temp_dir() . '/haml']); $path = tempnam(sys_get_temp_dir(), 'haml'); file_put_contents($path, $content); return $executor->render($path, $params); }
<?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";