WP Handlebars 1.0

Adding some WordPress sugar to the Handlebars PHP library.

xamin/handlebars.php

I’ve used the original handlebars.php library a lot and love it. Part of it is for the exchange that it allows between PHP and JavaScript code and part of it is for the separation of concerns that it allows to keep. A templating engine lends itself to be used to manage all the markup generation process but that’s not always the case with WordPress and its template hierarchy; sometimes a mix of Handlebars managed templates and default WordPress functions has to be used.
In these cases I could use some help.

While I’ve got plans for the wp-handlebars.php package I’ve laid out some basic functions only for the time being and covered the header, footer and sidebar template tags.

Usage

The repository use the xamin/handlebars.php library in its php-52 branch for back compatibility reasons.
Beside the WordPress specific helpers the WPHandlebars_Engine class can be used as a drop-in replacement for the Handlebars_Engine one.
As such the example from the xamin/handlebars.php library applies:

/* templates/page.handlebars template */

{{#header}}

<div id="primary" class="content-area">
    <main id="main" class="site-main" role="main">

        {{#each this}}
            {{#each posts}}
                {{> content}} 
                {{#if comments}}
                    {{> comments}} 
                {{/if}}
            {{/each}}
        {{/each}}

    </main><!-- #main -->
</div><!-- #primary -->

{{#sidebar}}
{{#footer}}

$engine = new WPHandlebars_Engine(array(
    'loader' => new Handlebars_Loader_FilesystemLoader(__DIR__.'/templates/'),
    'partials_loader' => new Handlebars_Loader_FilesystemLoader(
        __DIR__ . '/templates/',
        array(
            'prefix' => '_'
        )
    )
));

echo $engine->render(
    'page',
    get_posts()
)

On GitHub

See more on the package repository.