-
-
Save eddieajau/9a9f9778ecab0106f772 to your computer and use it in GitHub Desktop.
Mustache Service Provider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
); | |
} | |
} |
+1
What is Mustache Service Provider?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello world!