codeschubser/bootstrap-twig-components-bundle
Installation:
composer require codeschubser/bootstrap-twig-components-bundle
Register the bundle in config/bundles.php:
return [
// ...
Codeschubser\BootstrapTwigComponentsBundle\BootstrapTwigComponentsBundle::class => ['all' => true],
];
First Use Case: Render a basic Bootstrap-aligned button in a Twig template:
{{ button({
label: 'Click Me',
type: 'primary',
size: 'lg'
}) }}
Verify the output matches Bootstrap’s styling (e.g., btn-primary class).
Key Files:
templates/components/ (default Twig component templates).config/packages/bootstrap_twig_components.yaml (customization).Component Composition:
Chain components for complex UIs (e.g., a card with button and alert):
{{ card({
header: 'Title',
body: 'Content',
footer: button({ label: 'Action', type: 'outline-danger' })
}) }}
Dynamic Data Binding: Pass variables from controllers:
return $this->render('page.html.twig', [
'items' => $items,
'user' => $user,
]);
Use in Twig:
{% for item in items %}
{{ badge({ text: item.status, variant: item.isActive ? 'success' : 'warning' }) }}
{% endfor %}
Icon Integration:
Embed icons via icon parameter (supports Bootstrap Icons/Font Awesome):
{{ button({
label: 'Save',
icon: 'fa-solid fa-floppy-disk',
iconPosition: 'left'
}) }}
Slot-Based Layouts:
Use slot for reusable content blocks (e.g., modals):
{{ modal({
title: 'Confirm',
size: 'sm'
}) }}
{{ slot('content') }}
<p>Are you sure?</p>
{{ endslot }}
Asset Management: Ensure Bootstrap CSS/JS is loaded (e.g., via Webpack Encore or Symfony UX Turbo).
{{ encore_entry_link_tags('app') }}
Custom Templates:
Override default templates by copying from vendor/codeschubser/bootstrap-twig-components-bundle/templates/components/ to templates/components/.
Configuration:
Adjust defaults in config/packages/bootstrap_twig_components.yaml:
bootstrap_twig_components:
default_icon_set: 'bootstrap' # or 'fontawesome'
components:
button:
default_type: 'secondary'
Experimental Status:
composer.json:
"codeschubser/bootstrap-twig-components-bundle": "^1.0.0"
Icon System Quirks:
Accessibility Gaps:
aria-* attributes manually. Some components (e.g., modals) may need custom ARIA roles:
{{ modal({ aria: { 'aria-label': 'User Settings' } }) }}
Template Overrides:
php bin/console cache:clear
Component Output:
Use Twig’s dump to inspect rendered HTML:
{{ dump(_context) }}
Check for missing classes or malformed attributes.
Dependency Conflicts: If styles break, verify Bootstrap’s CSS isn’t being overridden. Use browser dev tools to inspect the computed styles.
Custom Components:
Extend the bundle by creating new Twig components in templates/components/:
{# templates/components/my_component.html.twig #}
<div class="my-component {{ variant }}">
{{ content }}
</div>
Register in services.yaml:
twig:
paths: ['%kernel.project_dir%/templates/components']
Dynamic Variants: Add logic to support dynamic variants (e.g., for alerts):
{% macro alert(content, variant='info') %}
{{ include('components/alert.html.twig', {
variant: variant,
content: content
}) }}
{% endmacro %}
Symfony UX Integration: Combine with Symfony UX for reactive components:
{{ button({
label: 'Toggle',
onclick: "this.textContent = 'Toggled'"
})|raw }}
{% block %} to lazy-load heavy components (e.g., data tables).$cachedComponent = $this->get('twig')->createTemplate('...')->render([
'data' => $data,
]);
$this->get('cache')->set('component_key', $cachedComponent, 3600);
How can I help you explore Laravel packages today?