Skip to main content
drupalreleases

Use direct release data with your AI assistant.

Learn more

Single File Components

Security covered Under active development 37 sites

This module enables developers to define frontend components within a single file, combining Twig, CSS, JavaScript, and PHP. These components function similarly to preprocess functions, integrating traditional Drupal theming with component-based design principles.

The Single File Components module allows developers to provide frontend components in a single file. These components contain Twig, CSS, JS, and PHP code which acts similar to a preprocess function.

The purpose of this module is to combine traditional Drupal theming with some elements of component based design.

Example code

Excited, confused, scared, and want to skip to some example code? Check out the documented files by clicking here!

Feature highlights

  • Put your Twig, CSS, JS, and PHP in one file, then use it like any other template (i.e. {% include "sfc--say-hello.html.twig" %}). Components can be defined as PHP classes or .vue style .sfc files. (link)
  • Library definitions are automatically generated and included in your template, saving developer time
  • Easily override components in themes by replacing library definitions and overriding their templates (link)
  • Save time by writing JS in the ATTACH constant, which automatically wraps your code in a Drupal behavior and a jQuery.once call (link)
  • Derive blocks, layouts, and field formatters from components using annotations (link)
  • Provide backend actions without writing a route or controller(link)
  • Single file components are Drupal plugins, you can extend them, derive them, and alter their plugin definition. You also have access to dependency injection so components can be unit tested.
  • Component library available at /sfc/library for developers to stress test and play with components. (link)

Requirements

PHP 7+ is required if you're defining the LIBRARY or DEPENDENCIES constants.

Installation

This module has no dependencies, so to install it, download and install it!

Writing a component

The README.md file is the best place to look for technical documentation, but you can also check out the sfc_example sub-module for example plugins.

Sites built with Single File Components

Slack channel

Join me in Slack in the #single-file-components channel! Details on how to connect here: https://www.drupal.org/slack

Drupal 9–11

Ask your assistant about Single File Components

Check compatibility with your Drupal and PHP version, maintenance and security coverage, from current release data. How it works

Depends on

Dependencies of the latest stable release

No dependencies recorded for this project.

Required by

Tracked projects that depend on this one

No tracked projects depend on this one yet.

Activity

Tracked releases
2
Tracked since
Apr 2023
Latest release
1 year ago
Releases (12 mo)
0 ▼ from 1
Maintenance
Active

Releases

Version Type Core PHP Release date
8.x-1.12 Stable 9–11 Nov 23, 2024
8.x-1.11 Stable 9–10 Apr 9, 2023