Skip to content

Instantly share code, notes, and snippets.

@arnisjuraga
Last active April 3, 2020 05:02
Show Gist options
  • Save arnisjuraga/a3799e4267c7d97db9277c25ffd1deaf to your computer and use it in GitHub Desktop.
Save arnisjuraga/a3799e4267c7d97db9277c25ffd1deaf to your computer and use it in GitHub Desktop.
A small script with manual steps, how to convert simple TWIG templates back to PHP markup. Used for Opencart, sequence is important. Will be updated as used for any new template.

TODO: automated script

Regex search-replace

Static variables echo


# Strings
\{\{\s(.*)\s\}\}
<?=\$$1; ?>

# Arrays

# with space
\{\{\s(.*?)\.(.*?)\s\}\}
<?=\$$1['$2']; ?>

#without space
\{\{(.*?)\.(.*?)\}}
<?=\$$1['$2']; ?>

# {% if $a['b'] %}
([\{%])+\s+if\s+.*?(.*?)?\.(.*?)\s+%}
<?php if \(\$$2['$3']\) { ?>


# THEN try this Try this at first: 
### ([\{%])+\s+.*?(.*?)?\.(.*?)\s
### \$$2['$3']

# Careful, dots and spec symbosl in variables
\{\% if (\w*) \%\}
<?php if \(\$$1\) { ?>


Controls: FOR

# if in_array 2nd level
##\{% if (.*?)\.(.*?) in (.*?) %}
##<?php if(in_array(\$$1['$2'], \$$3)) { ?>


# 2nd level array
##\{% for (.*?) in (.*?)\.(.*?) %}
##<?php foreach(\$$2['$3'] as \$$1) { ?>

# 1st level
\{% for (.*?) in (.*?) %}
<?php foreach\(\$$2 as \$$1\) { ?>

# Elses
\{% else %}
<?php } else { ?>

# end of if and for
\{% (endfor|endif) %}
<?php } ?>

# Ifs
\{% if (.*) %}
<?php if\(\$$1\) { ?>

# IF Arrays again
\{%\sif\s(.*?)\.(.*?)\s==\s(.*)\s%\}
<?php if\(\$$1['$2'] == $3 \) { ?>

# Just an array
(\h)(\w+)\.(\w+)
 \$$2['$3']





@arnisjuraga
Copy link
Author

arnisjuraga commented Oct 24, 2019

\{%\sif\s(.*?)\s==\s(.*)\s%\}
<?php if\(\$$1 == $2 \) { ?>

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