sylius/theme-bundle
Sylius Theme Bundle brings theme management to Symfony apps. Define and switch themes, organize templates and assets per theme, and build storefronts with clean overrides. Part of the Sylius ecosystem, with docs included and MIT licensed.
Installation
Add the bundle to your composer.json:
composer require sylius/theme-bundle
Enable it in config/bundles.php:
return [
// ...
Sylius\Bundle\ThemeBundle\SyliusThemeBundle::class => ['all' => true],
];
Basic Configuration
Define themes in config/packages/sylius_theme.yaml:
sylius_theme:
themes:
default: "@SyliusTheme/default"
custom: "@AppTheme/custom"
First Use Case
Override a Twig template for the default theme:
mkdir -p templates/bundles/SyliusTheme/default/_partials
cp vendor/sylius/theme-bundle/Resources/views/default/_partials/navbar.html.twig templates/bundles/SyliusTheme/default/_partials/
Edit the copied file to customize the navbar.
vendor/sylius/theme-bundle/Resources/views/ for base templates.config/packages/sylius_theme.yaml for theme paths and defaults.Sylius\Bundle\ThemeBundle\Twig\ThemeExtension for dynamic theme switching.@AppTheme/custom to extend @SyliusTheme/default.templates/bundles/AppTheme/custom).default).sylius_theme.yaml:
themes:
custom: "@AppTheme/custom"
{{ parent() }} in Twig to inherit blocks from parent themes.ThemeExtension in Twig:
{% set currentTheme = app.theme.getCurrentTheme() %}
ThemeContext service to dynamically resolve themes:
# config/services.yaml
Sylius\Bundle\ThemeBundle\Resolver\ThemeResolver:
arguments:
$themeContext: '@app.theme_context' # Custom service
ThemeContext:
class CustomThemeContext implements ThemeContextInterface {
public function getCurrentTheme(): string {
return request()->user() ? 'user_theme' : 'default';
}
}
public/bundles/AppTheme/custom/assets/.<link rel="stylesheet" href="{{ asset('bundles/AppTheme/custom/assets/style.css') }}">
templates/components/ (e.g., templates/components/alert.html.twig).{% include '@components/alert.html.twig' %}
php bin/console sylius:theme:create custom
(Extend with custom commands if needed.)Caching Issues
php bin/console cache:clear
debug:config to verify theme paths:
php bin/console debug:config sylius_theme
Template Path Priority
templates/bundles/<ThemeNamespace>/<ThemeName>/templates/bundles/SyliusTheme/<ThemeName>/@AppTheme/custom → templates/bundles/AppTheme/custom/).Symfony 6+ Asset Component
config/packages/sylius_theme.yaml:
sylius_theme:
asset_path: '%kernel.project_dir%/public/bundles'
{{ asset('bundles/AppTheme/custom/image.png') }} in Twig.Twig parent() in Non-Inherited Templates
parent() in standalone templates.Locale-Specific Themes
_<locale> to the theme name (e.g., default_en, default_fr).sylius_theme.yaml:
themes:
default_en: "@SyliusTheme/default_en"
{{ dump(app.theme.getCurrentTheme()) }}
php bin/console debug:twig
APP_DEBUG=true in .env to see template inheritance traces.Custom Theme Resolver
ThemeResolverInterface to implement logic like:
Event Listeners
sylius.theme.switch to log or modify theme switches:
use Sylius\Bundle\ThemeBundle\Event\ThemeSwitchEvent;
public function onThemeSwitch(ThemeSwitchEvent $event) {
// Custom logic here
}
Theme Configuration
ThemeConfiguration class to add theme-specific settings (e.g., color schemes).Webpack/Vite Integration
sylius-theme entries in webpack.config.js:
Encore
.addEntry('sylius-theme-default', './assets/themes/default.js')
.addEntry('sylius-theme-custom', './assets/themes/custom.js');
v2.0) for backward compatibility.atoms/, molecules/, organisms/ for scalability.sylius_shop templates).How can I help you explore Laravel packages today?