Theme Controller

TsgcHTMLThemeController / TsgcHTMLThemeBuilder — manage light, dark and system themes across all pages and generate Bootstrap CSS variable blocks for custom brand colours.

TsgcHTMLThemeController & TsgcHTMLThemeBuilder

TsgcHTMLThemeController reads a cookie or query parameter to determine which Bootstrap theme to activate and injects the required initialisation script. TsgcHTMLThemeBuilder generates a <style> block that overrides Bootstrap CSS variables with your brand colours.

Component classes

TsgcHTMLThemeController, TsgcHTMLThemeBuilder

Role

Light/dark theming & custom CSS

Languages

Delphi, C++ Builder, .NET

Switch themes, inject custom colours

Use TsgcHTMLThemeController to read a cookie or query parameter and apply the correct Bootstrap theme attribute. Use TsgcHTMLThemeBuilder to emit a custom CSS variable block for your brand colours.

uses
  sgcHTML_ThemeController;

// Detect theme from request cookie:
var
  oTheme: TsgcHTMLThemeController;
begin
  oTheme := TsgcHTMLThemeController.Create(nil);
  try
    oTheme.CookieName    := 'app_theme';
    oTheme.DefaultTheme  := htSystem;
    // Inject into the Bootstrap template:
    oTemplate.HeadContent :=
      oTemplate.HeadContent + oTheme.GetThemeScript(ARequest);
  finally
    oTheme.Free;
  end;
end;

// Generate brand CSS variables:
var
  oBuilder: TsgcHTMLThemeBuilder;
begin
  oBuilder := TsgcHTMLThemeBuilder.Create(nil);
  try
    oBuilder.PrimaryColor   := '#7C3AED';
    oBuilder.SecondaryColor := '#1E293B';
    oTemplate.HeadContent :=
      oTemplate.HeadContent + oBuilder.Build;
  finally
    oBuilder.Free;
  end;
end;
TsgcHTMLThemeController *oTheme = new TsgcHTMLThemeController(NULL);
try
{
  oTheme->CookieName   = "app_theme";
  oTheme->DefaultTheme = htSystem;
  oTemplate->HeadContent += oTheme->GetThemeScript(ARequest);
}
__finally { delete oTheme; }

TsgcHTMLThemeBuilder *oBuilder = new TsgcHTMLThemeBuilder(NULL);
try
{
  oBuilder->PrimaryColor = "#7C3AED";
  oTemplate->HeadContent += oBuilder->Build();
}
__finally { delete oBuilder; }
var themeCtrl = new TsgcHTMLThemeController();
themeCtrl.CookieName   = "app_theme";
themeCtrl.DefaultTheme = TsgcHTMLTheme.htSystem;
template.HeadContent += themeCtrl.GetThemeScript(request);

var themeBuilder = new TsgcHTMLThemeBuilder();
themeBuilder.PrimaryColor = "#7C3AED";
template.HeadContent += themeBuilder.Build();

Key properties & methods

The members you reach for most often.

TsgcHTMLThemeController.CookieName

Name of the cookie that stores the user's theme preference (htLight / htDark / htSystem).

TsgcHTMLThemeController.DefaultTheme

Fallback theme when no cookie or query parameter is present.

GetThemeScript

Reads the active theme from the request (cookie or ?theme= parameter) and returns the Bootstrap theme initialisation script to inject into <head>.

TsgcHTMLThemeBuilder.PrimaryColor

Overrides Bootstrap's --bs-primary CSS variable with your brand colour.

TsgcHTMLThemeBuilder.CustomVars

Raw CSS custom-property declarations appended to the generated <style> block for any Bootstrap or app-level variable.

TsgcHTMLThemeBuilder.Build

Returns a <style> block containing the :root overrides; assign to TsgcHTMLTemplate_Bootstrap.HeadContent.

Keep exploring

Online HelpFull API reference and usage guide for this component.
All sgcHTML ComponentsBrowse the full feature matrix of 60+ components.
Download Free TrialThe 30-day trial ships the 60.HTML demo projects.
PricingSingle, Team and Site licenses with full source code.

Ready to Get Started?

Download the free trial and start building web UIs in Delphi, C++ Builder and .NET.