Skip to content

Instantly share code, notes, and snippets.

@eddieajau
Created April 11, 2014 06:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eddieajau/9a9f9778ecab0106f772 to your computer and use it in GitHub Desktop.
Save eddieajau/9a9f9778ecab0106f772 to your computer and use it in GitHub Desktop.
Mustache Service Provider
<?php
$container = new Container;
$container->registerServiceProvider(new \Providers\MustacheServiceProvider);
// ...
$mustache = $container->get('mustache');
$emailView = $mustache->loadTemplate('email');
// Note that Mustache does not like looping over associative arrays.
$buffer = $emailView->render($params);
$message = (new \Swift_Message())
->setContentType('text/html')
->setSubject('Subject')
->setBody($buffer)
->setTo(array($job->replyto))
->setFrom(array('me@example.com' => 'Me'));
$mailer = new \Swift_Mailer(new \Swift_SmtpTransport);
$result = $mailer->send($message);
<?php
namespace Providers;
use Joomla\DI\Container;
use Joomla\DI\ServiceProviderInterface;
/**
* Registers the Mustache service provider.
*
* @since 1.0
*/
class MustacheServiceProvider implements ServiceProviderInterface
{
/**
* Get a Mustache object.
*
* @param Container $c A DI container.
*
* @return \Mustache_Engine
*
* @since 1.0
*/
public function getMustache(Container $c)
{
$config = $c->get('config');
$mustache = new \Mustache_Engine(array(
'loader' => new \Mustache_Loader_FilesystemLoader(
$config->get('mustache.views', __DIR__ . '/../templates'),
array(
'extension' => $config->get('mustache.ext', '.tmpl'),
)
),
));
$mustache->addHelper(
'number',
array(
'1f' => function ($value)
{
return sprintf('%.1f', $value);
},
)
);
return $mustache;
}
/**
* Registers the service provider within a DI container.
*
* @param Container $container The DI container.
*
* @return void
*
* @since 1.0
*/
public function register(Container $container)
{
$that = $this;
$container->set(
'mustache',
function ($c) use ($that)
{
return $that->getMustache($c);
}
);
}
}
@drmmr763
Copy link

Hello world!

@AshKyd
Copy link

AshKyd commented Jun 17, 2014

+1

@rgmears
Copy link

rgmears commented Jun 18, 2014

What is Mustache Service Provider?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment