Installation Add the bundle via Composer:
composer require dvp/gabarit-bundle
Enable the bundle in config/bundles.php (Symfony):
Dvp\GabaritBundle\DvpGabaritBundle::class => ['all' => true],
Basic Usage The bundle provides Twig extensions for common formatting tasks. Test it by creating a simple Twig template:
{{ 'Hello World'|gabarit_format }}
Ensure your Twig environment is configured to autoload extensions (handled automatically by the bundle).
First Use Case
Use gabarit_format to apply French typography rules (e.g., spacing, punctuation) to text:
{{ 'Bonjour, comment ça va ?'|gabarit_format }}
Output: Properly formatted French text with correct spacing around punctuation.
Text Formatting Apply typography rules to dynamic content:
{% for message in messages %}
{{ message.text|gabarit_format }}
{% endfor %}
Conditional Formatting
Use Twig’s if to apply formatting selectively:
{% if is_french %}
{{ text|gabarit_format }}
{% else %}
{{ text }}
{% endif %}
Integration with Forms Format user-generated content (e.g., comments) before display:
<div class="comment">
{{ comment.body|gabarit_format }}
</div>
Custom Extensions Extend the bundle’s functionality by creating a custom Twig extension that leverages its core logic:
// src/Twig/AppExtension.php
class AppExtension extends \Twig\Extension\AbstractExtension
{
public function getFunctions()
{
return [
new \Twig\TwigFunction('custom_format', [$this, 'customFormat']),
];
}
public function customFormat($text)
{
// Reuse GabaritBundle's logic or wrap it
return \Dvp\GabaritBundle\Twig\GabaritExtension::format($text);
}
}
gabarit_format filter in form themes to auto-format labels or help text.{{ 'greeting'|trans|gabarit_format }}
Twig Auto-Reloading If Twig extensions aren’t recognized, clear the cache:
php bin/console cache:clear
Character Encoding
Ensure input text is UTF-8 encoded to avoid formatting issues (e.g., mb_convert_encoding in PHP).
Performance
Avoid applying gabarit_format to large blocks of text in loops without caching. Cache the result if reused:
{% cache 'formatted_text' %}
{{ long_text|gabarit_format }}
{% endcache %}
Edge Cases The bundle may not handle all French typography rules (e.g., complex ligatures). Test with edge cases like:
{{ '«Bonjour !» — dit-il.'|gabarit_format }}
Extension Availability: Verify the extension is registered by dumping Twig’s available filters:
{{ dump(_twig_functions) }}
Look for gabarit_format.
Input/Output Mismatch: Compare raw input vs. formatted output to identify unexpected behavior:
<pre>{{ dump('Input: ' ~ text ~ ' | Output: ' ~ text|gabarit_format) }}</pre>
Custom Rules
Extend the core formatting logic by subclassing Dvp\GabaritBundle\Twig\GabaritExtension and overriding methods like format().
New Filters Add more Twig filters by extending the bundle’s extension class:
// Add to Dvp\GabaritBundle\Twig\GabaritExtension
public function getFilters()
{
return [
new \Twig\TwigFilter('custom_rule', [$this, 'customRule']),
// ...
];
}
Event Listeners
Hook into Symfony events (e.g., kernel.response) to auto-format responses globally.
How can I help you explore Laravel packages today?