Skip to main content
Drupal is a registered trademark of Dries Buytaert
Release: Views Bootstrap 5.5.4 — Minor update available for module views_bootstrap (5.5.4). Release: Rightup theme 1.0.3 — Minor update available for theme vartheme_bs5_rightup (1.0.3). Release: CommentOn 1.0.6 — Minor update available for module commenton (1.0.6). Release: ChatGPT Ads 1.0.3 — Minor update available for module chatgpt_ads (1.0.3). Release: Opensolr Search 5.1.3 — Minor update available for module opensolr_search (5.1.3). Release: ServiceM8 Webform Integration 1.2.1 — Minor update available for module servicem8_webform (1.2.1). Release: PostfixAdmin 1.0.0 — Initial release available for module postfix_admin (1.0.0)! Release: ip_analyser 1.0.3 — Minor update available for module ip_analyser (1.0.3). Module Revived: Swagger-PHP OpenAPI 3 documentation generator 1.0.0 — Module swagger_php updated after 10 months of inactivity (1.0.0). Security Coverage: Microsoft Azure AI — Module ai_provider_azure now has official Drupal security advisory coverage.

Twig Typography

Security covered

Part of the Twig ecosystem · 26 projects

View on drupal.org

This module provides a Twig filter which integrates the PHP Typography library and applies it to strings. An example use case is for protecting against widowed words in titles, or to process straight quotes into curly quotes. This could be applied in the page-title.html.twig template like this:

<h1{{ title_attributes.addClass('page-title') }}>{{ title|typography }}</h1>

The PHP Typography library can do the following transformations:

  • Hyphenation — over 50 languages supported
  • Space control, including:
    • widow protection
    • gluing values to units
    • forced internal wrapping of long URLs & email addresses
  • Intelligent character replacement, including smart handling of:
    • quote marks (‘single’, “double”)
    • dashes ( – )
    • ellipses (…)
    • trademarks, copyright & service marks (™ ©)
    • math symbols (5×5×5=53)
    • fractions (1⁄16)
    • ordinal suffixes (1st, 2nd)
  • CSS hooks for styling:
    • ampersands,
    • uppercase words,
    • numbers,
    • initial quotes & guillemets.

For more information on the PHP Typography library visit: https://github.com/mundschenk-at/php-typography

Requirements

This module requires no modules outside of Drupal core.

Installation

Install the Twig Typography module as you would normally install a contributed Drupal module. Visit https://www.drupal.org/node/1897420 for further information.

Configuration

  • Navigate to Administration > Extend and enable the module and its dependencies.
  • Within the module directory there is a file called typography_defaults.example.yml. This file can be copied and renamed to typography_default.yml and placed in the root of your custom theme. You can then modify the YML file to provide your own defaults for the typography filter to use. See class-settings.php for possible options.

Usage

A typography filter is provided and is used on strings with the pipe character:

{{ title|typography }}

The filter can also operate on render arrays and will try to use the page level render() function first (with bubbleable metadata) and fall back to renderPlain() if render isn't allowed.

The typography filter can accept parameters to modify the defaults. For example, to render the page title with typographic enhancements but without the de-widows functionality:

{{ title|typography({'set_dewidow': FALSE}) }}

These inline options are overrides of the defaults and are provided by a Twig hash. You can have multiple overrides in this hash. An alternative is to set your defaults in your theme, see the next section for details.

Configuration

Within the module directory there is a file called typography_defaults.example.yml. This file can be copied and renamed to typography_default.yml and placed in the root of your custom theme. You can then modify the YML file to provide your own defaults for the typography filter to use. See class-settings.php for possible options.

Similar modules

Typogrify: The Typogrify module provides a text format as a way of applying typographic enhancements rather than a Twig based approach. It uses a different 3rd party library.

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
1
Tracked since
Jul 2024
Latest release
2 years ago
Releases (12 mo)
0
Maintenance
Dormant

Releases

Version Type Core Release date
2.1.3 Stable Jul 8, 2024