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); }