clippings/mustache-provider

A Mustache service provider for Pimple and Silex.

2.0.0-rc.3 2020-02-19 08:27 UTC

This package is auto-updated.

Last update: 2024-11-19 18:28:03 UTC


README

Provides Mustache integration for the Pimple dependency-injection container or the Silex application micro-framework.

Installation

Add clippings/mustache-provider to your project's Composer dependencies:

composer require clippings/mustache-provider

Configuration

  • mustache.path (optional): Path to the directory containing Mustache template files.

  • mustache.partials_path (optional): Path to the directory containing Mustache partial template files. If none is specified, this will default to mustache.path.

  • mustache.partials (optional): An associative array of template names to template contents. Use this if you want to define partials inline.

  • mustache.helpers (optional): An associative array of Mustache helpers. See the Mustache.php helpers documentation for more information.

  • mustache.options (optional): An associative array of Mustache options. See the Mustache.php documentation for more information.

Services

  • mustache.loader (optional): A Mustache template loader instance. This loader will use the mustache.path option you provided. You can also replace the loader with something awesome of your own.

  • mustache.partials_loader (optional): The Mustache template loader used to load partials. By default, this will load templates from either your the mustache.partials_path or mustache.partials configuration options. You can also replace the partials loader with another loader of your choice.

Registering

<?php

$app->register(new Mustache\MustacheServiceProvider, array(
    'mustache.path' => __DIR__.'/../views',
    'mustache.options' => array(
        'cache' => __DIR__.'/../tmp/cache/mustache',
    ),
));

Usage

The Mustache provider provides a mustache service:

<?php

$app->get('/hello/{name}', function ($name) use($app) {
    return $app['mustache']->render('hello', array(
        'name' => ucfirst($name),
    ));
});

This will render the hello.mustache file from your application's views directory.

Template Loading

The Mustache service provider ships with a Filesystem template loader enabled by default. All you have to do to get things off the ground is set a mustache.path option to tell it where your templates live. But you can swap that loader out for any other Mustache Loader.

For +10 Awesome, you should check out the Inline template loader:

<?php

// ...

$app->register(new MustacheServiceProvider, array(
    'mustache.loader' => new Mustache_Loader_InlineLoader(__FILE__, __COMPILER_HALT_OFFSET__)
));

$app->get('/{name}', function($name) use ($app) {
    return $app['mustache']->render('hello', compact('name'));
})
->value('name', 'world');

// ...

__halt_compiler();

@@ hello
Hello, {{ name }}!

The Trait

Mustache\MustacheApplicationTrait adds a render helper to your app:

<?php

use Silex\Application;

class MyApplication extends Application
{
    use \Mustache\MustacheApplicationTrait;
}

$app = new MyApplication;

Now you can just call render:

<?php

return $app->render('hello', array('name' => 'Justin'));

Or BYO Response:

<?php
$response = new Response;
$response->setTtl(10);

return $app->render('hello', array('name' => 'Justin'), $response);

It also provides a renderTemplate helper which returns a rendered string instead of a Response object.

Customization

You can mess with Mustache before using it by extending the mustache service:

<?php

$app['mustache'] = $app->factory($app->extend('mustache', function ($mustache, $app) {
    $mustache->addHelper('app', $app);
    $mustache->setLogger($app['monolog']);

    return $mustache;
}));