dnl/mthaml
Laravel package integrating Haml templating via mthaml, enabling you to render .haml views with a concise, Ruby-like syntax. Useful for teams adopting Haml in PHP/Laravel projects and keeping view markup clean and expressive.
Install the Package
composer require dnl/mthaml
First HAML File
Create a .haml file in resources/views/ (e.g., welcome.haml):
%html
%head
%title Welcome
%body
%h1= greeting
%p= message
Render in a Controller
use Dnl\MtHaml\MtHaml;
public function showWelcome()
{
$haml = new MtHaml();
$output = $haml->renderFile(
'welcome.haml',
['greeting' => 'Hello, Laravel!', 'message' => 'This is HAML in PHP.']
);
return response($output);
}
Leverage Laravel’s View System (Optional)
Extend Laravel’s view resolver to handle .haml files:
// app/Providers/AppServiceProvider.php
use Illuminate\Support\Facades\View;
use Dnl\MtHaml\MtHaml;
public function boot()
{
View::addExtension('haml', function ($view, $compiler) {
$haml = new MtHaml();
$content = $haml->render($view->getPath(), $view->getData());
return $compiler->compileString($content);
});
}
Now use HAML like Blade:
return view('welcome.haml', ['greeting' => 'Hello']);
Template Inheritance
layout.haml):
%html
%head
%title= title
%body
= yield
home.haml):
- title = "Home Page"
%h1 Welcome
= yield
$haml->renderFile('home.haml', []);
Partials/Includes
_header.haml):
%header
%h2= title
- **Include in Parent**:
```haml
= render('_header.haml', { title: "Dashboard" })
View::make() for partials:
$header = view('partials.header', ['title' => 'Dashboard'])->render();
Dynamic Content
%ul
- foreach $items as $item
%li= $item
- if $show_button
%button Click Me
Multi-Target Output
$haml->setTarget('twig')->renderFile('template.haml');
$twigContent = $haml->renderFile('template.haml', [], 'twig');
// Pass to Twig template engine or save to file.
Blade-Like Directives Create a custom HAML directive to mimic Blade:
- @if($condition)
%p This is a conditional block.
$haml->addDirective('@if', function ($args) {
return "<?php if({$args}): ?>";
});
Service Container Integration
Bind MtHaml to Laravel’s container:
$app->bind(MtHaml::class, function ($app) {
return new MtHaml(['cache_path' => storage_path('haml_cache')]);
});
use Illuminate\Support\Facades\App;
$haml = App::make(MtHaml::class);
Caching Compiled Templates Enable caching for performance:
$haml = new MtHaml(['cache' => true, 'cache_path' => storage_path('haml_cache')]);
php artisan haml:clear # (Requires custom Artisan command)
Asset Pipeline Integration Use HAML for views in Laravel Mix/Vite:
%link{rel: "stylesheet", href: mix('css/app.css')}
mix() or @vite() directives are handled by a custom HAML filter.Indentation Sensitivity
%div # Wrong: Extra space causes syntax error
%p Content
Twig/Blade Syntax Conflicts
= for output conflicts with Twig’s {{ }} or Blade’s {!! !!}.
%p= $variable # Outputs raw value (no escaping)
{{ }} for safe output in Twig mode:
%p {{ $variable }} # Escaped output (Twig target)
Dynamic Content Quirks
$var) may not auto-escape like Blade’s {!! !!}.
%p= $user->name # Unescaped HTML possible
$haml->setEscapeFunction('htmlspecialchars');
Partial Path Resolution
render() fails if partials aren’t in the expected path.
= render('partials/_header') # Fails if path is wrong
$haml->setPartialPath('resources/views/partials');
Caching Gotchas
php artisan view:clear # Clears Laravel's view cache (may not affect HAML)
// app/Console/Commands/ClearHamlCache.php
public function handle()
{
$haml = new MtHaml(['cache_path' => storage_path('haml_cache')]);
$haml->clearCache();
}
Enable Verbose Output
$haml = new MtHaml(['debug' => true]);
Check Compiled Output Temporarily save compiled templates to debug:
$haml->setTarget('php')->renderFile('template.haml', [], true); // Save to file
IDE Support
.haml files.Common Errors & Fixes
| Error | Cause | Solution |
|---|---|---|
ParseError: syntax error |
Invalid HAML syntax | Validate with haml-lint or IDE. |
Undefined variable $var |
Variable not passed to template | Ensure data is passed to renderFile(). |
File not found |
Incorrect partial path | Use absolute paths or configure base dir. |
Twig Error: [Syntax] ... |
HAML-to-Twig conversion issue | Check Twig syntax rules in HAML docs. |
FatalError: Class not found |
Missing PHP class |
How can I help you explore Laravel packages today?