PHP code example of devtheorem / php-handlebars

1. Go to this page and download the library: Download devtheorem/php-handlebars library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

devtheorem / php-handlebars example snippets


use DevTheorem\Handlebars\Handlebars;

$source = <<<'HBS'
    <p>Hi {{user.name}}, you have {{notifications.length}} new notification(s):</p>
    <ul>
    {{#notifications}}
        <li>{{count}} {{message}} ({{time}})</li>
    {{/notifications}}
    </ul>
    HBS;

$data = [
    'user' => ['name' => 'Jane'],
    'notifications' => [
        ['count' => 4, 'message' => 'new comments', 'time' => '5 min ago'],
        ['count' => 3, 'message' => 'new followers', 'time' => '1 hr ago'],
    ],
];

$template = Handlebars::compile($source);
echo $template($data);

use DevTheorem\Handlebars\Handlebars;

$templateDir = 'templates';
$cacheDir = 'templateCache';

foreach (glob("$templateDir/*.hbs") ?: [] as $file) {
    $name = basename($file, '.hbs');
    $code = Handlebars::precompile(file_get_contents($file));
    file_put_contents("$cacheDir/$name.php", " $code");
}

$template = title' => 'My Page', 'user' => ['name' => 'Jane']];
echo $template($data, [
    'partialResolver' => fn(string $name) => 

use DevTheorem\Handlebars\{Handlebars, Options};

$template = Handlebars::compile('Hi {{first}} {{last}}!', new Options(
    strict: true,
));

echo $template(['first' => 'John']); // Error: "last" not defined

use DevTheorem\Handlebars\{Handlebars, HelperOptions};

$template = Handlebars::compile('{{#equals my_var false}}Equal to false{{else}}Not equal{{/equals}}');
$helpers = [
    'equals' => function (mixed $a, mixed $b, HelperOptions $options) {
        // In JS, null is not equal to blank string or false or zero,
        // and when both operands are strings no coercion is performed.
        $equal = ($a === null || $b === null || is_string($a) && is_string($b))
            ? $a === $b
            : $a == $b;

        return $equal ? $options->fn() : $options->inverse();
    },
];
$runtimeOptions = ['helpers' => $helpers];

echo $template(['my_var' => 0], $runtimeOptions); // Equal to false
echo $template(['my_var' => 1], $runtimeOptions); // Not equal
echo $template(['my_var' => null], $runtimeOptions); // Not equal

use DevTheorem\Handlebars\{Handlebars, HelperOptions};

$template = Handlebars::compile('{{foo 2 "value"}}
{{#person}}{{firstName}} {{lastName}}{{/person}}');

$helpers = [
    'helperMissing' => function (...$args) {
        $options = array_pop($args);
        return "Missing {$options->name}(" . implode(',', $args) . ')';
    },
    'blockHelperMissing' => function (mixed $context, HelperOptions $options) {
        return "'{$options->name}' not found. Printing block: {$options->fn($context)}";
    },
];

$data = ['person' => ['firstName' => 'John', 'lastName' => 'Doe']];
echo $template($data, ['helpers' => $helpers]);

composer