Twig Templates: Component-Driven Design in Drupal 8, 9, 10, 11

Highlights

  • Drupal, paired with Twig templates, eases website management and design consistency via component-driven design.
  • In component-driven design, website pieces are treated as separate, reusable modules for efficient updates and consistent user experiences.
  • Storybook, an open-source tool, facilitates isolated building, documenting, and testing of components.
  • Twig, Drupal's templating engine, aids in HTML rendering and separates business from presentation logic.
  • Storybook and Twig together streamline workflow, reducing duplication and maintaining brand alignment.
twig templates

Twig Templates, Drupal’s Rendering Mechanism

Our websites are under enormous pressure to perform. Audiences expect fast load times, brand consistency, and a seamless user experience on every page, regardless of the device they’re using. Organizations from nonprofits and government agencies to enterprises with global audiences often need to deploy new pages, microsites, or campaigns quickly without sacrificing quality.

Drupal, with its robust content management capabilities, is a natural fit for these complex digital ecosystems. Yet the traditional approach to theming in Drupal can make it difficult to maintain visual cohesion and scale efficiently. Large, page-specific templates can lead to duplication of work, inconsistent branding, and longer release cycles.

Component-driven design offers a better way. By breaking the user interface into discrete, reusable components—each with its own markup, styling, and behavior—you create a library of building blocks that can be combined in countless ways to construct pages. When those components are built as Twig templates, which are Drupal’s rendering mechanism, and previewed through Storybook, which serves as a living component catalog, you align design and development in a single, efficient workflow.

Understanding Component-Driven Design

Component-driven design treats every piece of a website, whether it’s a button, a navigation bar, a testimonial card, or a hero banner, as a standalone module. Each is built once, styled once, and documented so it can be reused in multiple contexts without being rebuilt from scratch.

In a traditional Drupal theming approach, developers might create a separate template for each page type, embedding markup and styles directly into the larger template. This makes it difficult to maintain consistency. A single change to a design element, like a button style, might require edits in dozens of files. Over time, subtle inconsistencies creep in, resulting in a fractured brand experience.

With a component-driven approach, each button, header, or card is defined as its own component, stored in one place, and used wherever needed. Updating the component updates every instance of it across the site. The result is a more consistent user experience and a reduced maintenance burden.

Storybook: The Living Component Catalog

Storybook is an open-source tool for building, documenting, and testing components in isolation. While it began in the JavaScript ecosystem, it can be adapted for Drupal’s Twig templates, turning it into a powerful environment for theming teams. If you want a Drupal-native approach, the Storybook contrib module works directly with Single Directory Components (SDC) and .stories.twig files, letting you preview the exact Twig templates Drupal will render—without maintaining a custom Twig loader.

In Storybook, components are displayed independently of the rest of the site. Designers, developers, and stakeholders can view them in different states such as a button in default, hover, or disabled without navigating Drupal. This speeds up development because components can be refined without loading the full application and allows earlier stakeholder review.

Storybook also supports add-ons for accessibility testing, visual regression testing, and responsive previews. By integrating these into a CI pipeline, teams can catch accessibility and style regressions before changes go live.

Twig: The Power Behind Drupal’s Presentation Layer

Twig is Drupal’s templating engine, responsible for rendering HTML output. It separates presentation from business logic, a philosophy that aligns perfectly with component-driven design. Twig supports template inheritance, includes smaller templates within larger ones, and safely escapes output by default to reduce XSS risks.

When used in a component-driven setup, each component’s markup lives in a dedicated Twig file, and its variables are defined in Drupal’s render arrays or schema files. In Storybook, matching mock data ensures previews reflect production output accurately.

The Case for Marrying Storybook and Twig

In many teams, design and development still work in parallel and only converge late in the process. Even with modern design tools like Figma providing CSS snippets and interactive prototypes, there’s still a gap between the design spec and production-ready, accessible Twig templates. Without a shared component library, designers review static mocks, front-end developers build HTML/CSS, and Drupal themers re-implement that logic—leading to duplication and mismatches.

By integrating Storybook with Twig—ideally through Drupal’s Storybook contrib module and SDC—you eliminate much of that duplication. The component in Storybook is the same one used in Drupal. Designers and stakeholders can approve it in Storybook, confident it will match the final site, while developers and QA teams work from a single source of truth.

Setting Up a Storybook–Twig–Drupal Workflow

If you’re on Drupal 10.3 or later, start with Single Directory Components. Organize each component in its own folder with Twig, schema, styles, scripts, and a .stories.twig file for Storybook. Install the Storybook contrib module to bridge your components directly into Storybook, keeping markup and data in sync.

Point Storybook at your theme’s components directory so it renders the same Twig Drupal does, and ensure both environments share the same compiled CSS and JavaScript for visual consistency. Keep mock data aligned with Drupal’s variables so what you see in Storybook matches production output. For a ready-made design system approach, consider Emulsify or Wingsuit, which bundle Drupal theming, Storybook, and front-end build tooling.

Business Benefits That Go Beyond Development

Beyond the technical gains, this workflow speeds up content and marketing efforts. Teams can launch new pages and campaigns using pre-approved building blocks, without waiting for custom development. Brand consistency improves because all components come from the same library, and maintenance costs drop—updates to a component automatically cascade site-wide.

For organizations exploring decoupled or headless Drupal, a Storybook+Twig component library, backed by design tokens, can be reused in other contexts like mobile apps or JavaScript front ends.

Common Challenges and How to Overcome Them

Adopting a component-driven approach requires a mindset shift. Start with a small set of high-value components and expand gradually. Avoid duplicating styles or scripts between Storybook and Drupal—share a single build pipeline. Keep mock data synchronized with Drupal’s render arrays or schema to prevent discrepancies, and store design tokens centrally to maintain brand alignment across platforms.

Future-Proofing the Component Library

A component library should evolve with automated guardrails. Run accessibility and visual regression tests in CI on every pull request so regressions are caught early. Keep design tokens in a platform-agnostic format (like JSON or YAML) so they can be compiled into variables for both Drupal and Storybook. This approach ensures consistency and makes reuse straightforward in headless or hybrid environments.

How New Target Can Help

At New Target, we help organizations break their themes into reusable components, set up Storybook with Twig and SDC, and train teams to maintain and grow their libraries. Our approach aligns design, Drupal development, and content workflows so sites are faster to build, easier to maintain, and more consistent for users.

If your organization is ready for a modern front-end workflow that’s built to last, let’s chat.

Twig Templates, Drupal’s Rendering Mechanism Our websites are under enormous pressure to perform. Audiences expect fast load times, brand consistency, and a seamless user experience on every page, regardless of...

Today, speed isn’t just a luxury—it’s a necessity. Websites and applications that load instantly not only deliver a superior user experience but also rank higher in search engine results and...

In digital marketing, time is money—and nowhere is that more obvious than in the creation of landing pages. Every campaign, promotion, product launch, or lead-generation push needs a landing page...

Large language models (LLMs) like OpenAI’s ChatGPT and Google’s Gemini are redefining search. The first impression of your brand might not come from your PR, content marketing, website, or press...

Ready for more?

Subscribe to our newsletter to stay up to date on the latest web design trends, digital marketing approaches, ecommerce technologies, and industry-specific digital solutions.

Name