Browse files

Covert more smarty tags to twig

  • Loading branch information...
1 parent e6b0e30 commit 0e6d963ce95f407b351eb93347c6fc5a5fcb79f2 Drak committed Jul 17, 2012
Showing with 76 additions and 5 deletions.
  1. +8 −5 twigify.php
  2. +68 −0 twigify_lib.php
View
13 twigify.php
@@ -3,9 +3,9 @@
require_once __DIR__.'/twigify_lib.php';
if (isset($argc)) {
- if ($argc != 2) {
+ if ($argc != 3) {
die("
-Usage: php twigify.php <filename>
+Usage: php twigify.php <filename> <output_filename>
Run from the browser puts it into test mode.
");
@@ -17,13 +17,14 @@
$fileName is not readable or non-existing
");
}
+ $outFileName = $argv[2];
$string = file_get_contents($fileName);
} else {
$string = file_get_contents(__DIR__.'/web/system/SearchModule/Resources/views/User/form.tpl');
}
-$string = str_replace('{/else}', '{% else %}', $string);
+$string = str_replace('{else}', '{% else %}', $string);
$string = str_replace('{/if}', '{% endif %}', $string);
$string = str_replace('{/foreach}', '{% endfor %}', $string);
$string = str_replace('{/foreachelse}', '{% else %}', $string);
@@ -32,6 +33,8 @@
$twig = new Twiggifier($string, $matches);
$twig->stripString('|safetext');
$twig->replaceString('=$', '=');
+$twig->replaceString('|@json_encode', '|json_encode()');
+$twig->replaceString('|escape', '|e');
$res = preg_match_all('/\{\s{0,}(.+?)\s{0,}\}/', $twig->getTemplate(), $matches);
$twig = new Twiggifier($twig->getTemplate(), $matches);
@@ -51,8 +54,8 @@
}
if (isset($fileName)) {
- file_put_contents($fileName.'.new', $twig->getTemplate());
- echo "File written\n";
+ file_put_contents($outFileName, $twig->getTemplate());
+ echo "$outFileName written.\n";
} else {
echo "<pre>";
echo htmlentities($twig->getTemplate());
View
68 twigify_lib.php
@@ -27,6 +27,51 @@ public function replaceString($search, $replace)
}
}
+ private function comment($match, $key)
+ {
+ $string = "{# $match #}";
+ $this->template = str_replace($this->matches[0][$key], $string, $this->template);
+ }
+
+// public function __call($method, $args)
+// {
+// $this->comment($args[0], $args[1]);
+// }
+
+ public function convertAjaxheader($match, $key)
+ {
+ $this->comment($match, $key);
+ }
+
+ public function convertMath($match, $key)
+ {
+ $this->comment($match, $key);
+ }
+
+ public function convertButton($match, $key)
+ {
+ $this->comment($match, $key);
+ }
+
+ public function convertImg($match, $key)
+ {
+ $this->comment($match, $key);
+ }
+
+ public function convertIcon($match, $key)
+ {
+ $this->comment($match, $key);
+ }
+
+ public function convertInclude($match, $key)
+ {
+ preg_match('/^include file=(?:"{0,1}|\'{0,1})(.+?)(?:"{0,1}|\'{0,1})\s{0,}$/', $match, $matches);
+
+ $fileName = str_replace('"', '', str_replace("'", '', $matches[1]));
+ $string = "{% include '!!!$fileName' %}";
+ $this->template = str_replace($this->matches[0][$key], $string, $this->template);
+ }
+
public function convertForeach($match, $key)
{
preg_match('/^foreach (.+?)\s{0,}$/', $match, $matches);
@@ -64,6 +109,29 @@ public function convertForeach($match, $key)
$this->template = str_replace($this->matches[0][$key], $string, $this->template);
}
+ public function convertCycle($match, $key)
+ {
+ preg_match('/^cycle (.+?)\s{0,}$/', $match, $matches);
+
+ $values = ''; // value
+ if (preg_match('/values=(?:"{0,1}|\'{0,1})(.+?)(?:"{0,1}|\'{0,1})(?:\s|$)/', $matches[1], $itemMatches)) {
+ $values = $itemMatches[1];
+ }
+ $values = str_replace("'", '', $values);
+
+ $parts = explode(',', $values);
+ $string = '[';
+ foreach ($parts as $skey => $value) {
+ $string .= "$value";
+ if ($skey < count($parts)-1) {
+ $string .= ',';
+ }
+ }
+ $string = "{{ cycle($string]), i }}";
+
+ $this->template = str_replace($this->matches[0][$key], $string, $this->template);
+ }
+
public function convertIf($match, $key, $command = 'if')
{
// {if ....}

0 comments on commit 0e6d963

Please sign in to comment.