Problem/Motivation

For some time we've been talking about moving away from preprocess - via Twig autoescape and other things. This didn't really happen to the extent people hoped during the 8.x cycle though.

So at this point we have a lot of preprocess implementations, and a lot of templates relying on variables prepared by them.

We can't remove preprocess in 8.x, and we can't make variables that were previously available to templates unavailable.

However, we can potentially rationalize specific preprocess implementations, deprecate the use of particular variables etc.

template_preprocess() gets called for every single template render, to generate exactly the same variables to pass in - this would be a good candidate to remove.

Additionally we may be able to define twig variables as objects for other things, so that they're used on demand in the template, rather than always generating strings in preprocess as we currently do. Something like node $submitted would be a good candidate for that.

Proposed resolution

Replace the variables added by template_preprocess() with Twig global variables: http://symfony.com/doc/current/cookbook/templating/global_variables.html

Remaining tasks

User interface changes

API changes

Data model changes

Comments

catch created an issue. See original summary.

catch’s picture

Issue tags: +Twig, +Performance
Wim Leers’s picture

Issue tags: +Needs profiling

+1 for the principle, but not yet sure how we'll gain much:

  1. Not sure yet whether using Twig's "global variables" feature will gain us performance
  2. Not sure yet whether it's all that valuable to get rid of just one preprocess function

Definitely an interesting idea that's worth exploring :)

Cottser’s picture

Title: Replace template_process() and possibly other things with Twig global variables » Replace template_preprocess() and possibly other things with Twig global variables
Related issues: +#2611246: [PP-1] Document the recommended method of creating file URLs to theme assets (e.g. images)

Title fix and adding a related issues that talked about using Twig global vars.

dawehner’s picture

Well for 8.1.x it could be tricky, given that other preprocess functions might rely on the existence of those variables, like 'attributes'.

catch’s picture

Attributes will be hard to change, but I think something like $is_front is unlikely to be changed in preprocess, and something like node $submitted we might be able to consider the specific variables passed to a preprocess function @internal.

Another way to do it would be to have Stable provide the preprocessed variables since that's the bc layer for themes.

So there might be a way to get around it.

joelpittet’s picture

Regarding attributes yes it's tricky, but not impossible. I've got the content_attributes and title_attributes mostly sorted here #2108771: Remove special cased title_attributes and content_attributes for Attribute creation

Version: 8.1.x-dev » 8.2.x-dev

Drupal 8.1.0-beta1 was released on March 2, 2016, which means new developments and disruptive changes should now be targeted against the 8.2.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.2.x-dev » 8.3.x-dev

Drupal 8.2.0-beta1 was released on August 3, 2016, which means new developments and disruptive changes should now be targeted against the 8.3.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.

Version: 8.3.x-dev » 8.4.x-dev

Drupal 8.3.0-alpha1 will be released the week of January 30, 2017, which means new developments and disruptive changes should now be targeted against the 8.4.x-dev branch. For more information see the Drupal 8 minor version schedule and the Allowed changes during the Drupal 8 release cycle.