[PP-1] Document the recommended method of creating file URLs to theme assets (e.g. images)

Created on 8 November 2015, over 9 years ago
Updated 7 July 2025, 14 days ago

Problem/Motivation

In working on a new D8 theme I found that the {{ base_dir }} variable is available in page.html.twig, but not html.html.twig - I'm not sure if that is by design, or an omission, but it would be helpful to have in conjunction with {{ active_theme_path() }} (which does work in html.html.twig).

Proposed resolution

Using base paths/base directories in file URLs is hugely problematic. We have a Twig function for this: file_url().

The remaining problem then is that you don't know the location of the theme. This is what ✨ Add stream wrappers to access extension files Needs work solves: it allows you to use theme://theme_name as a file URI, which file_url() will transform into a file URL. Using a theme:// file URI works always, regardless of the exact location of the theme. (For example: /sites/all/themes/my_theme, /sites/mysite.com/themes/my_theme, /profiles/my_profile/my_theme …)

Remaining tasks

User interface changes

None.

API changes

None.

Data model changes

None.

πŸ“Œ Task
Status

Postponed

Version

11.0 πŸ”₯

Component

theme system

Created by

πŸ‡ΊπŸ‡ΈUnited States jpamental

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024