Create alternative to Twig include function to improve Drupal integration

Created on 13 October 2016, about 8 years ago
Updated 14 March 2023, over 1 year ago

Problem/Motivation

There's some momentum on component based theming based on Drupal. One of the most popular ways to deal with this is to include external files from a Twig template. For example:

{{ include("card.html.twig", {
    title: label,
    text: body,
  },
  with_context: false,
) }}

Obviously, this doesn't allow any kind of preprocessing in the preprocess functions or theme suggestions. If there would be an easy way to load those templates using theme system, we wouldn't necessarily have to lose all that functionality.

Proposed resolution

Create a Twig function that will include a Twig template while also running Drupal's normal preprocess functions and theme suggestions.

The new function should be as close to the syntax of Twig include, and shouldn't be much more difficult to use so that people would replace their includes with this new function.

Since Drupal's Twig templates will automatically take {{ render_array }} and render it to a string, the easiest and most flexible way for this new Twig function to work is for the function to return a render array that uses [ '#theme' => 'theme_hook_name', '#var1' => 'etc' ]. Drupal's render API will then call the theme registry to find the appropriate template and process all of its variables, etc.

An idea for how this could look in a Twig template.

{{
  include( "card", {
    title: label,
    text: body,
  }
) }}

Which would then be in PHP:

print render([
  '#theme' => 'card',
  '#title' => $label,
  '#text' => $body,
]);

Remaining tasks

User interface changes

API changes

Data model changes

Feature request
Status

Needs work

Version

10.1

Component
Theme 

Last updated about 14 hours ago

Created by

🇫🇮Finland lauriii Finland

Live updates comments and jobs are added and updated live.
  • Needs subsystem maintainer review

    It is used to alert the maintainer(s) of a particular core subsystem that an issue significantly impacts their subsystem, and their signoff is needed (see the governance policy draft for more information). Also, if you use this tag, make sure the issue component is set to the correct subsystem. If an issue significantly impacts more than one subsystem, use needs framework manager review instead.

  • Needs framework manager review

    It is used to alert the framework manager core committer(s) that an issue significantly impacts (or has the potential to impact) multiple subsystems or represents a significant change or addition in architecture or public APIs, and their signoff is needed (see the governance policy draft for more information). If an issue significantly impacts only one subsystem, use Needs subsystem maintainer review instead, and make sure the issue component is set to the correct subsystem.

  • Needs frontend framework manager review

    Used to alert the fron-tend framework manager core committer(s) that a front-end focused issue significantly impacts (or has the potential to impact) multiple subsystems or represents a significant change or addition in architecture or public APIs, and their signoff is needed (see the governance policy for more information). If an issue significantly impacts only one subsystem, use Needs subsystem maintainer review instead, and make sure the issue component is set to the correct subsystem.

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