Skip to content

Instantly share code, notes, and snippets.

@putzflorian
Last active January 6, 2023 14:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save putzflorian/219f582377b20d64d97ea9d8751dbb89 to your computer and use it in GitHub Desktop.
Save putzflorian/219f582377b20d64d97ea9d8751dbb89 to your computer and use it in GitHub Desktop.
Migrate Php templates to Twig for Pimcore X
__PARTS__
Methods (required!!!)
((?:render|extends|include)\s*\(?\s*(?:'|")):?
Level
(?:((?:\w|-|_)+)(?::|\/))
Last Level (required!!!)
(?:((?:\w|-|_)+)(?::|\/)?)
Extension (required!!!)
(\.html\.twig)
Advanced (if other code is in between the template string)
('.+')
__PLACEHOLDERS__
Methods
$1 (no / after)
Level x
$x+1/
Last Level x
$x+1 (no / after)
Extension
$x (no / slash before)
Advanced
$x (position in string)
__BASIC STRUCTURE__
Find
Methods Levels(times x) Last Level Advanced(if needed) Extension
Replace
$1$2/....$x$x+1$x+2
__SHORT__
((?:render|extends|include)\s*\(?\s*(?:'|")):?(?:((?:\w|-|_)+)(?::|\/))(?:((?:\w|-|_)+)(?::|\/)?)(\.html\.twig)
$1$2/$3$4
__MEDIUM__
((?:render|extends|include)\s*\(?\s*(?:'|")):?(?:((?:\w|-|_)+)(?::|\/))(?:((?:\w|-|_)+)(?::|\/))(?:((?:\w|-|_)+)(?::|\/)?)(\.html\.twig)
$1$2/$3/$4$5
__LARGE__
((?:render|extends|include)\s*\(?\s*(?:'|")):?(?:((?:\w|-|_)+)(?::|\/))(?:((?:\w|-|_)+)(?::|\/))(?:((?:\w|-|_)+)(?::|\/))(?:((?:\w|-|_)+)(?::|\/)?)(\.html\.twig)
$1$2/$3/$4/$5$6
__HUGE__
((?:render|extends|include)\s*\(?\s*(?:'|")):?(?:((?:\w|-|_)+)(?::|\/))(?:((?:\w|-|_)+)(?::|\/))(?:((?:\w|-|_)+)(?::|\/))(?:((?:\w|-|_)+)(?::|\/))(?:((?:\w|-|_)+)(?::|\/)?)(\.html\.twig)
$1$2/$3/$4/$5/$6$7
__ADVANCED__
((?:render|extends|include)\s*\(?\s*(?:'|")):?(?:((?:\w|-|_)+)(?::|\/))(?:((?:\w|-|_)+)(?::|\/)?)('.+')(\.html\.twig)
@mysliwietzflorian
Copy link

Hi, thanks for these helping regexes.

Some suggestions:

  • Maybe add some explanation for short, medium, etc., twig syntax. How does short, medium, etc., syntax look like?
    What are the new conventions?

  • Add possibly a hint that all regexes cover different syntaxes.

  • Transform .txt file to .md file and use markdown features

  • Some tags and functions in twig are currently not supported here. (only form_theme was relevant for us, hint would be nice)

  • Warning about template_from_string

  • Ternary syntax is not supported. E.g.: {% extends editmode ? 'foo.html.twig' : 'bar.html.twig' %}

  • Shorthand syntax with two colons is not supported. E.g.: {% include '::test/foo.html.twig' %}

    • Or without further path elements {% include '::foo.html.twig' %}

@kovinet
Copy link

kovinet commented Jan 6, 2023

This doesn't solve cases like <?= $this->input("headline", ['placeholder' => 'Headline']); ?>, right?

Are there any expressions available for converting PHP syntax to TWIG syntax?

@mysliwietzflorian
Copy link

Hello @kovinet!

If you have stumbled across this collection during the update of Pimcore 6.9 to Pimcore X keep in mind:

  • If I remember correctly, this will only find some changes in the Twig syntax and their namespace resolution (especially for render and include functionalities).
    • e.g.: {% extends ':Layout:default.html.twig' %} to {% extends 'Layout/default.html.twig' %}
  • At this point in the update process we have already migrated all/most of our PHP templates. This will probably be a manual process but you can help yourself by searching for specific keywords in the project (<?=).
  • The major problem during this migration process are not the syntax changes but that you can have a lot of logic in the PHP templates that you cannot (and shouldn't) have in Twig. This will be the most time consuming change, in my opinion.

TLDR; no it does not migrate PHP templates to twig templates but updates the Twig namespace syntax.

Good luck with the update!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment