Problem/Motivation

Right now, Twig is bootstrapped/configured via the TwigFactory class. This makes almost impossible to cleanly add extensions on Twig or change the loader.

Proposed resolution

The proposed patch :

* removes the factory and replaces it with proper configuration in the container;
* moves all custom filters/node visitors/tags/functions to a new Drupal Twig extension;
* the render function has been renamed to drupal_render to avoid collision with the same function from the Twig bridge in Symfony. AFAIU, this is only used internally and as such, it should not be a problem.
* the loader is now a service (twig.loader.filesystem) and an alias has been made to twig.loader (which is used by the Twig service). This allows for better flexibility (you can add your own loader for instance by replacing the twig.loader service with a Twig_Loader_Chain one that keeps the default filesystem loader and your own).

CommentFileSizeAuthor
#5 twig_refactoring_5.patch10.61 KBfabpot
twig_refactoring.patch8.89 KBfabpot
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

Crell’s picture

Status: Needs review » Needs work

This makes sense, I think. The registerTwig() method needs a docblock, however.

chx’s picture

While this is very very lovely, calling render drupal_render is going to be somewhat confusing -- but as it's not exposed to the outside world, it's OK. Here's why: render also includes a show() command which drupal_render doesn't.

fabpot’s picture

I don't have any preferences for the drupal_render name. Any suggestions?

Crell’s picture

drender()? render_array()? render_struct()? (I don't have a strong preference, just throwing out ideas.)

fabpot’s picture

FileSize
10.61 KB

I've renamed drupal_render to render_var, which probably best describes what it does (and it is kind of symmetric with the twig_render_template function) .

I've also added a phpdoc to the registerTwig() method as requested.

fabpot’s picture

Status: Needs work » Needs review
chx’s picture

Status: Needs review » Reviewed & tested by the community

I like it.

chx’s picture

Much more than that: thanks for doing this, it was on the roadmap all along but we never got around to do this properly.

webchick’s picture

Status: Reviewed & tested by the community » Fixed

I don't totally know what this is doing, but I figure if it looks good to fabpot and chx, then who am I to argue? :D

Committed and pushed to 8.x. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

c4rl’s picture

Issue tags: +Twig, +Twig engine

Tagging