Skip to content

refactor(templating): add TwigRenderer skeleton - #1980

Merged
JohnVillalovos merged 1 commit into
mainfrom
jlvillal/twig_stage1
Nov 19, 2026
Merged

JohnVillalovos merged 1 commit into
mainfrom
jlvillal/twig_stage1

Conversation

@JohnVillalovos

Copy link
Copy Markdown
Collaborator

Add the Twig implementation of TemplateRenderer and the factory that creates its Twig environment. Nothing uses either class yet.

TwigEnvironmentFactory creates an environment that escapes output for HTML by default and renders an undefined variable as empty, as Smarty does. Compiled templates are cached in subdirectories of tpl_c/twig, so TemplateCacheDirectory::Flush() now removes subdirectories too. It only deleted files directly in tpl_c, which would have left the Twig cache in place after an administrator cleared the cache.

TwigRenderer looks templates up in tpl/ and in the directory of the language that is current when it is created. A directory added later is searched last, as with Smarty. The renderer follows the cache.templates setting. The ?debug query parameter switches the template cache off only in debug mode, so a visitor cannot do it.

This is a skeleton:

  • fetchLocalized() throws. The localized lookup is added together with the first email templates.
  • a template is rendered under the name it is asked for. Mapping a .tpl name to its .twig file and falling back to Smarty come with the engine selection.

The tests pin two behaviours next to those of SmartyRenderer:

  • variables passed to render() are not kept, unlike with Smarty
  • a control template does not see the assigned variables, as with Smarty

Assisted-by: Claude:claude-fable-5-2

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

A critical active finding shows that the bootstrap can fatally fail on multiple web entry points because it eagerly loads TwigRenderer before Composer's autoloader is registered.

1 open issue
What changed in this PR

Adds the initial Twig templating infrastructure alongside the existing Smarty renderer, including environment configuration, renderer behavior, recursive template-cache cleanup, and focused compatibility tests.

Changes:

  • Added configurable Twig environment creation with HTML escaping, undefined-variable handling, debug behavior, and compiled-template caching.
  • Added the initial Twig renderer with template lookup, variable handling, control rendering, language-directory support, and validators.
  • Updated template-cache flushing to remove nested Twig cache directories and added corresponding tests.
File Description
tests/​src/​Common/​Templating/​TwigEnvironmentFactoryTest.php Tests Twig environment defaults, escaping, undefined variables, directory filtering, debug behavior, and cache settings.
tests/​Infrastructure/​Common/​TwigRendererTest.php Tests Twig renderer behavior, template lookup, variables, controls, language directories, validation, and caching.
tests/​Application/​Admin/​TemplateCacheDirectoryTest.php Tests removal of Smarty files and nested Twig compiled-template directories.
src/​Common/​Templating/​TwigEnvironmentFactory.php Creates configured Twig environments and filters unavailable template directories.
lib/​Common/​Templating/​TwigRenderer.php Provides the initial Twig implementation of the common TemplateRenderer contract.
lib/​Common/​namespace.php Loads TwigRenderer through the legacy common namespace bootstrap.
lib/​Application/​Admin/​TemplateCacheDirectory.php Recursively removes compiled template files and nested Twig cache directories.

Review effort: Lite

Comment thread lib/Common/namespace.php Resolved

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The change has two active moderate findings that reverse the intended template search order, causing later directories to override earlier ones in both added-path handling and initial loader construction.

2 open issues
1 resolved since last review

Review effort: Lite

Comment thread lib/Common/Templating/TwigRenderer.php Resolved
Add the Twig implementation of TemplateRenderer and the factory that
creates its Twig environment. Nothing uses either class yet.

TwigEnvironmentFactory creates an environment that escapes output for
HTML by default and renders an undefined variable as empty, as Smarty
does. Compiled templates are cached in subdirectories of tpl_c/twig,
so TemplateCacheDirectory::Flush() now removes subdirectories too. It
only deleted files directly in tpl_c, which would have left the Twig
cache in place after an administrator cleared the cache.

TwigRenderer looks templates up in tpl/ and in the directory of the
language that is current when it is created. A directory added later
is searched last, as with Smarty. The renderer follows the
cache.templates setting. The ?debug query parameter switches the
template cache off only in debug mode, so a visitor cannot do it.

This is a skeleton:

- fetchLocalized() throws. The localized lookup is added together
  with the first email templates.
- a template is rendered under the name it is asked for. Mapping a
  .tpl name to its .twig file and falling back to Smarty come with
  the engine selection.

The tests pin two behaviours next to those of SmartyRenderer:

- variables passed to render() are not kept, unlike with Smarty
- a control template does not see the assigned variables, as with
  Smarty

Assisted-by: Claude:claude-fable-5-2

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are identified for the Twig templating infrastructure changes.

No open issues 🎉

2 resolved since last review

Review effort: Lite

@JohnVillalovos
JohnVillalovos merged commit e550591 into main Nov 19, 2026
15 checks passed
@JohnVillalovos
JohnVillalovos deleted the jlvillal/twig_stage1 branch November 19, 2026 03:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants