ronan-gloo / jade-php

Jade template engine ported to PHP 5.3
https://github.com/visionmedia/jade
MIT License
26 stars 18 forks source link

short array syntax not compatible with PHP 5.3 #7

Open dessaya opened 10 years ago

dessaya commented 10 years ago

I got the following error using PHP 5.3.20:

[07-Apr-2014 13:37:21 Etc/GMT+3] PHP Parse error:  syntax error, unexpected '[' in jade.stream://data;<header><?php foreach ([1,2,3,4] as $item) { ... } ?> </header> on line 1

I managed to make it work with this hacky workaround in src/Jade/Compiler.php (replace [] syntax with array() syntax):

diff --git a/src/Jade/Compiler.php b/src/Jade/Compiler.php
index 233ccba..5849fc9 100644
--- a/src/Jade/Compiler.php
+++ b/src/Jade/Compiler.php
@@ -1005,6 +1005,8 @@ class Compiler
         } else {
             $code = $this->createCode('foreach (%s as %s) {',$node->obj,$node->value);
         }
+        $code = str_replace("[", "array(", $code);
+        $code = str_replace("]", ")", $code);

         $this->buffer($code);
kylekatarnls commented 10 years ago

But what happen if you do foreach($array[0] as $subArray) ? And if you have not PHP 5.4, so use directly the array() function in the templates, it's safer.

dessaya commented 10 years ago

I didn't understand your first question... where whould I try foreach($array[0] as $subArray)?

To clarify, in my jade template I have something like:

- each item in [1,2,3,4,5,6,7] 

I am not able to use the array() syntax in jade because some of my templates are also compiled in javascript (I know, large legacy project).

kylekatarnls commented 10 years ago

Sorry for my english.

I said you should do instead :

- each item in array(1, 2, 3, 4, 5, 6, 7)

Because if you need to do this elsewhere :

- each subItem in items["some-item"]

Youre str_replace will broke it, [ and ] can be array( and ) but offset selection too. So be carefull.

dessaya commented 10 years ago

Ok, I understand now :-) Yes, of course, my patch is not a proper fix. I'm not familiar with the code so I wasn't able to implement a correct patch. Sorry.