SE7ENSKY / jade2php

Unlock Jade for PHP! Convert Jade templates into raw PHP templates. CLI tool and JavaScript API. Test covered.
43 stars 10 forks source link

Object access #1

Closed khc closed 9 years ago

khc commented 9 years ago

There is a problem with compiling objects to PHP. The following Jade code:

- var a = array[0]
- var b = array['index']
- var c = object.property
- var d = object.method()

is compiled to:

<?php $a = $array[0] ?>
<?php $b = $array['index'] ?>
<?php $c = $object['property'] ?>
<?php $d = $object['method']() ?>

although I think the two last lines should look like:

<?php $c = $object->property ?>
<?php $d = $object->method() ?>

jade2php depends on your library js2php where the code is generated properly. I've checked using the online demo.

ivankravchenko commented 9 years ago

This particular thing is hard-coded here and preferred on my environment. I think this should be an option, correct?

khc commented 9 years ago

Yes, an option would be great. I'm using jade2php with WordPress and casting every object to array is troublesome, but accessing methods is impossible.

ivankravchenko commented 9 years ago

I am using with WordPress too, but with arrays (and some custom data model code) :)

ivankravchenko commented 9 years ago

Bumped version to 1.1.4. Added --no-arrays-only CLI option and arraysOnly: false option here. Please review and make me know it is what you requested :)

khc commented 9 years ago

Something not right (or I'm doing something wrong)

git clone https://github.com/SE7ENSKY/jade2php.git
cd jade2php
npm update
echo '- var a = object.property' | bin/jade2php --no-arrays-only --omit-php-runtime --omit-php-extractor

And the result is:

<?php $a = $object['property'] ?>

My environment: OSX 10.9.4, node 0.10.35 (installed with brew)

endel commented 9 years ago

Hey guys,

I've made some updates on js2php since the @ivankravchenko's fork. I suggest you to pull the changes if it doesn't break anything.

ivankravchenko commented 9 years ago

Thanks you, guys. @khc , that was my stupid code typo, fixed in 1.1.5 (5b9aa8d). @endel , switched to your original js2php implementation.

ivankravchenko commented 9 years ago

@endel , I suggest you check jsExpressionToPhp.coffee I've written this mess to handle some quirks. May be some of that work bring ideas on js2php improvements.

ivankravchenko commented 9 years ago

@khc , please let me know if current implementation is sufficient for you.

janwirth commented 9 years ago

i still get the object[key] notation in my rendered php files..

khc commented 9 years ago

Everything is working now:

echo '- var a = object.property' | jade2php --no-arrays-only --omit-php-runtime --omit-php-extractor

gives

<?php $a = $object->property ?>