Exemple #1
0
<?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";
Exemple #2
0
<?php

/**
 * This example shows how to integrate MtHaml with PHP templates.
 */
require __DIR__ . "/autoload.php";
$haml = new MtHaml\Environment('php');
/*
 * Compile the template to PHP
 */
$template = __DIR__ . '/example-php.haml';
$hamlCode = file_get_contents($template);
// no need to compile if already compiled and up to date
if (!file_exists($template . '.php') || filemtime($template . '.php') != filemtime($template)) {
    $phpCode = $haml->compileString($hamlCode, $template);
    $tempnam = tempnam(dirname($template), basename($template));
    file_put_contents($tempnam, $phpCode);
    rename($tempnam, $template . '.php');
    touch($template . '.php', filemtime($template));
}
/*
 * Execute the compiled template
 */
echo "\n\nExecuted Template:\n\n";
extract(['foo' => 'bar']);
require $template . '.php';
echo "\n\nRendered Template:\n\n";
readfile($template . '.php');
Exemple #3
0
<?php

/**
 * This example shows how to integrate MtHaml with Twig by
 * proxying the Twig Loader.
 *
 * Template files with a `.haml` extension, or whose code starts
 * with `{% haml %}` are parsed as HAML.
 */
require __DIR__ . "/autoload.php";
$haml = new MtHaml\Environment('twig', array('enable_escaper' => false));
$arrayLoader = new Twig_Loader_Filesystem(array(__DIR__));
/*
 * Use a custom loader as a proxy to the actual loader. The custom loader is
 * responsible of converting HAML templates to Twig, before returning them.
 */
$hamlLoader = new MtHaml\Support\Twig\Loader($haml, $arrayLoader);
$twig = new Twig_Environment($hamlLoader);
/*
 * Register the Twig extension. Compiled templates sometimes need this to
 * execute, depending on HAML features in use (some filters, some attributes).
 */
$twig->addExtension(new MtHaml\Support\Twig\Extension($haml));
/*
 * Execute the template:
 */
echo "\n\nExecuted Template:\n\n";
if (true) {
    // parsed as haml because of extension
    $twig->display('example-twig.haml', array());
} else {