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