contao-thememanager/core
Contao ThemeManager is a lightweight, forward-thinking CSS framework for Contao, built around components, flexbox layouts and CSS custom properties. Provides a solid theming foundation with a modern, maintainable approach.
Installation
composer require contao-thememanager/core
Ensure Contao CMS is installed (v4.13+ recommended) and the package is enabled in config/autoload.php:
'contrib' => [
'thememanager' => true,
],
First Use Case: Basic Theme Activation
my-theme) via the UI or manually to themes/.my-theme as default).CLI Quick Check Verify installation with:
php vendor/bin/contao-console thememanager:list
Outputs active/inactive themes.
Activation/Deactivation
Use the ThemeManager service to toggle themes programmatically:
$themeManager = \Contao\CoreBundle\Framework::getContainer()->get('thememanager.manager');
$themeManager->activateTheme('my-theme'); // Replace with your theme name
Theme Dependencies
Define dependencies in theme.xml (e.g., requires="bootstrap5"). The manager auto-blocks activation if dependencies are missing.
User-Specific Themes Override the default theme per user via a hook:
// config/config.php
$GLOBALS['TL_HOOKS']['loadDataContainer'][] = function() {
if (\Input::get('do') === 'themes') {
$user = \BackendUser::getInstance();
if ($user->theme !== '') {
\System::loadLanguageFile('tl_theme');
\Database::getInstance()->prepare("UPDATE tl_theme SET default=? WHERE name=?")
->execute($user->theme, $user->theme);
}
}
};
Frontend Theme Detection
Use the ThemeManager to fetch the active theme in templates:
{{ app.container.get('thememanager.manager').getActiveTheme().getName() }}
Extend theme metadata via theme.xml:
<theme name="my-theme" description="My Custom Theme">
<meta>
<author>Your Name</author>
<version>1.0.0</version>
<contributors>
<contributor>Contributor 1</contributor>
</contributors>
</meta>
<config>
<option name="dark_mode" type="boolean" default="false" label="Enable Dark Mode"/>
</config>
</theme>
Access config in PHP:
$theme = $themeManager->getTheme('my-theme');
$darkMode = $theme->getConfig('dark_mode', false);
Theme-Aware Modules Dynamically load CSS/JS based on the active theme:
$theme = $themeManager->getActiveTheme();
if ($theme->getName() === 'my-theme') {
$this->addCssFile('bundles/mytheme/css/style.css');
}
Fallback Themes
Set a fallback theme in config/config.php:
$GLOBALS['TL_THEMES'] = [
'fallback' => 'default-theme',
];
Caching Issues
php vendor/bin/contao-console cache:clear
thememanager:clear-cache for theme-specific cache:
php vendor/bin/contao-console thememanager:clear-cache
Theme Naming Conflicts
-) in theme names; use underscores (_) instead.if (!$themeManager->isValidThemeName('my-theme')) {
throw new \RuntimeException('Invalid theme name');
}
Dependency Hell
php vendor/bin/contao-console thememanager:validate
Log Theme Events
Enable debug mode in config/local.php:
'debug' => true,
Check logs for ThemeManager events in var/log/contao.log.
Inspect Active Theme Dump the active theme object:
\Contao\CoreBundle\Framework::getContainer()->get('debug.dump')->dump($themeManager->getActiveTheme());
Custom Theme Validators
Extend validation logic by implementing ThemeManagerValidatorInterface:
class CustomThemeValidator implements ThemeManagerValidatorInterface {
public function validate(Theme $theme): bool {
return $theme->getConfig('dark_mode') !== true;
}
}
Register in config/config.php:
$GLOBALS['TL_THEME_VALIDATORS'][] = CustomThemeValidator::class;
Post-Activation Hooks Trigger actions after theme activation:
$GLOBALS['TL_HOOKS']['afterThemeActivation'][] = function($themeName) {
\System::log("Theme '$themeName' activated!", __FILE__, TL_ERROR);
};
Theme Preview in Backend Override the preview template by copying:
vendor/contao-thememanager/core/src/Resources/contao/themes/preview.tpl
to templates/.
$themeManager->preloadTheme('my-theme');
thememanager:build to concatenate/minify theme assets:
php vendor/bin/contao-console thememanager:build --theme=my-theme
How can I help you explore Laravel packages today?