Installation:
composer require bobthecow/mustache-bundle
Verify the package appears in composer.json under require.
Register the Bundle:
Add to config/bundles.php (Symfony 4+):
Bobthecow\Bundle\MustacheBundle\BobthecowMustacheBundle::class => ['all' => true],
For Symfony <4, add to AppKernel.php:
new Bobthecow\Bundle\MustacheBundle\BobthecowMustacheBundle(),
Configure Templating Engine:
Update config/packages/framework.yaml (Symfony 4+):
framework:
templating:
engines: ['twig', 'mustache']
For Symfony <4, add to config.yml:
framework:
templating:
engines: ['twig', 'mustache']
First Template:
Create a Mustache file at templates/Hello/index.html.mustache:
<h1>Hello, {{ name }}!</h1>
Render in a controller:
return $this->render('Hello/index.html.mustache', ['name' => 'World']);
Template Rendering:
render() method in controllers:
return $this->render('Bundle:Path:template.mustache', $data);
/**
* @Template("Bundle:Path:template.mustache")
*/
public function showAction() { return ['key' => 'value']; }
Partial Templates: Include reusable components:
{{> partials/header.mustache }}
Store partials in templates/partials/.
Dynamic Logic:
Use Mustache’s built-in helpers (e.g., {{#if}}, {{^else}}):
{{#items}}
<li>{{.}}</li>
{{/items}}
Asset Integration: Combine with Symfony’s asset system:
<img src="{{ asset('images/logo.png') }}">
Pass asset() helper via controller:
return $this->render('template.mustache', [
'asset' => $this->get('assets.packages')->getUrlGenerator()
]);
Configuration Overrides:
Customize Mustache settings in config/packages/bobthecow_mustache.yaml:
bobthecow_mustache:
cache: true
partials_dir: '%kernel.project_dir%/templates/partials'
Cache Invalidation:
php bin/console cache:clear
config/packages/bobthecow_mustache.yaml:
bobthecow_mustache:
cache: false
Template Paths:
.mustache extension (e.g., template.html.mustache).Bundle:Controller:template.mustache format for consistency.Partial Loading:
partials (default) or configured via partials_dir.{{> partial }} are resolved from the template’s directory.Symfony 4+ Twist:
config/bundles.php.Deprecated Features:
league/mustache or symfony/mustache.Template Errors: Mustache throws generic errors. Enable Symfony’s profiler to inspect rendered output:
if ($this->get('kernel')->isDebug()) {
return new Response($this->renderView('template.mustache', $data));
}
Variable Dumping:
Use {{{dump}}} in templates to debug variables (requires custom helper):
$mustache->addHelper('dump', function($vars) {
return print_r($vars, true);
});
Configuration Validation:
Validate config/packages/bobthecow_mustache.yaml exists and is correctly formatted. Defaults:
bobthecow_mustache:
cache: true
partials_dir: '%kernel.project_dir%/templates/partials'
Custom Helpers: Register Mustache helpers in a compiler pass or service:
$mustache->addHelper('formatDate', function($date) {
return (new \DateTime($date))->format('Y-m-d');
});
Template Events:
Listen to mustache.render events (Symfony <4) to modify output:
$dispatcher->addListener('mustache.render', function($event) {
$event->setTemplate($event->getTemplate()->replace('{{title}}', 'Custom Title'));
});
Override Default Loader: Replace the template loader for custom paths:
bobthecow_mustache:
loader:
class: App\Mustache\CustomLoader
arguments: ['%kernel.project_dir%/custom_templates']
How can I help you explore Laravel packages today?