Installation
composer require alexvasilyev/mthaml-bundle
Add the bundle to config/bundles.php (Symfony 4+):
return [
// ...
Alexvasilyev\MtHamlBundle\AlexvasilyevMtHamlBundle::class => ['all' => true],
];
Basic Usage
Create a .haml file (e.g., resources/views/hello.haml):
%h1 Hello, {name}!
%p Welcome to HAML templating.
Render it in a controller:
use Alexvasilyev\MtHamlBundle\Templating\Engine\MtHamlEngine;
public function index(MtHamlEngine $hamlEngine)
{
return $hamlEngine->render('hello.haml', ['name' => 'World']);
}
First Use Case Replace Twig templates for views requiring cleaner syntax (e.g., forms, layouts). Example:
= form_start('path', 'post')
%label(for="email") Email
%input#email{:type => "email", :name => "email"}
= form_end()
View Rendering
MtHamlEngine in controllers/services.resources/views/ (e.g., user/show.haml).$hamlEngine->render('user/show.haml', ['user' => $userData]);
Layouts and Inheritance
haml_include for partials:
= haml_include('partials/header.haml', {title: 'Page Title'})
haml_layout (if supported by the engine).Asset Integration
Asset component for static files:
%link{:rel => "stylesheet", :href => asset('css/style.css')}
Form Handling
= form_row('text', 'username', {label: 'Username', value: $user->username})
form_row() helper for seamless integration with Symfony’s FormBuilder.path() → url_for()).MtHamlEngine in PHPUnit:
$this->mockBuilder()
->disableOriginalConstructor()
->getMock()
->method('render')
->willReturn('<h1>Test</h1>');
Deprecation Warnings
foreach syntax).Namespace Conflicts
index.html.haml vs. index.html.twig).Limited Documentation
php bin/console cache:clear) if templates fail to update.Custom Filters
Add filters via services.yaml:
alexvasilyev_mthaml.twig.filter.my_filter:
class: App\Twig\MyFilterExtension
tags:
- { name: 'alexvasilyev_mthaml.filter' }
Override Engine
Extend MtHamlEngine for custom logic (e.g., auto-escaping):
class CustomHamlEngine extends MtHamlEngine
{
protected function escape($str) { /* custom logic */ }
}
Asset Pipeline Integrate with Webpack Encore for preprocessing:
%script{:src => mix('js/app.js')}
assetic to precompile HAML to HTML for static sites.How can I help you explore Laravel packages today?