Problem/Motivation
One of the things that #3064854: Allow Twig templates to use front matter for metadata support made clear is that our current implementation of Twig makes a lot of assumptions; specifically around not being consumed outside of Drupal. This makes it more difficult for 3rd party software to consume Twig templates (originally intended for core/contrib) as doing so requires a specific environment and varying extensions created for use in Drupal.
Current 3rd party implementations are left to recreate/mimic said code on their own, e.g.: https://github.com/pattern-lab/plugin-drupal-twig-components
It would be nice if they could simply require a single Drupal component that allowed them to integrated more easily.
Proposed resolution
Move our current Twig environment and extensions into the \Drupal\Component\Twig namespace; providing a way to tie into core (when it's present) or gracefully fail when it's not.
Remaining tasks
TBD
User interface changes
None
API changes
TBD
Data model changes
None
Release notes snippet
TBD
Comments
Comment #2
aleksip@markcarver Thank you for creating this issue! I am very interested in helping make this happen. Do you have any thoughts about the way to tie into core? Something like
class_exists()to detect available core classes?An alternative that comes to mind is to create an
AbstractDrupalTwigExtensionwith concrete implementations ofgetFunctions(),getFilters()etc. referencing abstract functions. The component could then have aComponentDrupalTwigExtensionsubclass with non-core implementations and core would have aCoreDrupalTwigExtensionsubclass with core implementations.Comment #3
markhalliwellI'm not entirely too sure yet. Twig is very tightly coupled to certain services at the moment.
This issue is more of a placeholder to revisit after 9.0.0 has been released as anything done here will likely change things up quite a bit. Hopefully, we can do it in a way that is BC which seems possible given how strict Symfony/Twig is with their APIs.
If I were to venture a guess, I would say that the component version would contain the base methods that "do nothing" (when core services aren't available). And the core version would subclass said component and add in the necessary services as to not break the existing Twig service/class implementations in core.
Comment #4
xjmThese would be minor-only changes. Since 8.9.x and 9.0.x are now in beta, I'm moving this to 9.1.x. Thanks!