Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

# Arrays

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

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

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

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


# Strings
\{\{\s(.*)\s\}\}
<?=\$$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

This comment has been minimized.

Copy link
Owner 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
You can’t perform that action at this time.