Talesoft / tale-jade

A complete and fully-functional implementation of the Jade template language for PHP
http://jade.talesoft.codes
MIT License
88 stars 10 forks source link

Code blocks #19

Closed jacmoe closed 8 years ago

jacmoe commented 8 years ago

I get this error when I try to use block buffered code:

Failed to parse jade: You should indent in by one level only

This kind of code can reproduce the error:

        -
          $menuItems[] = ['label' => 'Logout (' . Yii::$app->user->identity->username . ')',
                         'url' => ['/site/logout'],'linkOptions' =>['data-method' => 'post']]
jacmoe commented 8 years ago

Ideally, I would like to be able to just indent like this:

        -$menuItems[] = ['label' => 'Logout (' . Yii::$app->user->identity->username . ')',
                         'url' => ['/site/logout'],'linkOptions' =>['data-method' => 'post']]

I am aware that it is not completely valid Jade - I think - but MtHaml allows this. It would make for tighter code.

jacmoe commented 8 years ago

Should probably also be fixed for unbuffered code?

= and !=

TorbenKoehn commented 8 years ago

I will have a look into this!

The current engine actually parses the indentation of unbuffered code and strips it to keep the internal level updated to be able to break out of the code-block on outdentation.

You can use the following three styles right now:

unindented:

- $menuItems[] = ['label' => 'Logout (' . Yii::$app->user->identity->username . ')', 'url' => ['/site/logout'],'linkOptions' =>['data-method' => 'post']]

indented by one level only:

- $menuItems[] = ['label' => 'Logout (' . Yii::$app->user->identity->username . ')', 
    'url' => ['/site/logout'],'linkOptions' =>['data-method' => 'post']]

indented on multiple levels:

- $menuItems[] = [
    'label' => 'Logout (' . Yii::$app->user->identity->username . ')', 
    'url' => ['/site/logout'],
    'linkOptions' =>[
        'data-method' => 'post'
    ]];

To get the last bracket indented correctly, you have to move down one level first

- 
    $menuItems[] = [
        'label' => 'Logout (' . Yii::$app->user->identity->username . ')', 
        'url' => ['/site/logout'],
        'linkOptions' =>[
            'data-method' => 'post'
        ]
    ];

I might ignore the indentation level here in a future version!

jacmoe commented 8 years ago

Yes, but now I have a clearer picture of what is wrong.

error

jacmoe commented 8 years ago

See the indentation? The relevant source code:

      if (Yii::$app->user->isGuest)
        -$menuItems[] = ['label' => 'Signup', 'url' => ['/site/signup']]
        -$menuItems[] = ['label' => 'Login', 
          'url' => ['/site/login']]
      else
        -$menuItems[] = ['label' => 'Users', 'url' => ['/user/index']]

I have 'pretty' turned on. Perhaps I should test it without..

TorbenKoehn commented 8 years ago

I'll test that right now and fix it, let me check.

jacmoe commented 8 years ago

Just for good measure:

      -$menuItems[] = ['label' => 'Issues',
        'url' => ['/issue/index']]
      if (Yii::$app->user->isGuest)
        -$menuItems[] = ['label' => 'Login', 
          'url' => ['/site/login']]
        -$menuItems[] = ['label' => 'Users', 'url' => ['/user/index']]
      else
        -$menuItems[] = ['label' => 'Gii', 'url' => ['/gii']]

That code works

If you don't code block right before an else conditional, all is well. :)

EDIT: scrap that - the indentation is still wonky. Sorry about the noise. :)

TorbenKoehn commented 8 years ago

I just updated Tale Jade and added smaller tests for conditionals, especially one handling your case specifically

https://github.com/Talesoft/tale-jade/commit/d51e1479526fa9e21774babbd78d3e10a4d7df97#diff-c07b4ba7fabccce1a86b24aac62cba6fR49

It passes on all supported PHP versions https://travis-ci.org/Talesoft/tale-jade/builds/89463155

It's working fine with correct indentation. Make sure you always indent the same way and either one level up, one level down or stay on the same level.

Could you send me the complete file you used? That way I may take a look at it and check if we can avoid that problem in the future somehow.

Thanks for testing :)

Tell me if I can close this issue (or you might close it yourself)

jacmoe commented 8 years ago

I will close this issue and if it becomes a real problem I will see if I can make a minimal reproducible test case. Perhaps there is wonkiness either in my file or the implementation ( Yii 2 framework).

Thanks for making this library :)

closing