XaminProject / handlebars.php

Handlebars processor for php
331 stars 132 forks source link

"if" block helper stopped working with "else" #4

Closed joshpangell closed 11 years ago

joshpangell commented 11 years ago

The if else block helper was previously working correctly, but since the previous release 3079518 (fixing the looping issue), it no longer works.

Note: all work with 600215d

Testing methods

// If
$h = new Handlebars_Engine;

echo $h->render(
    'If:
    <div class="entry">
      {{#if author}}
        <h1>{{author.firstName}} {{author.lastName}}</h1>
      {{else}}
        <h1>Unknown Author</h1>
      {{/if}}
    </div>
    ', 
    array(
        'author' => array(
            'firstName' => "Charles", 
            'lastName' => "Jolley"
        )
    )
);

// Else
$h = new Handlebars_Engine;

echo $h->render(
    'Else:
    <div class="entry">
      {{#if author}}
        <h1>{{author.firstName}} {{author.lastName}}</h1>
      {{else}}
        <h1>Unknown Author</h1>
      {{/if}}
    </div>
    ', 
    array(
        'author' => false
    )
);

// If with loop
$h = new Handlebars_Engine;

echo $h->render(
    'If with a loop:
    <div class="entry">
      {{#if author}}
        {{#author}}<h1>{{firstName}} {{lastName}}</h1>{{/author}}
      {{else}}
        <h1>Unknown Author</h1>
      {{/if}}
    </div>
    ', 
    array(
        'author' => array(
            array(
                'firstName' => "Nikola", 
                'lastName' => "Tesla"
            ),
            array(
                'firstName' => "Carl", 
                'lastName' => "Segan"
            ),
            array(
                'firstName' => "Neil", 
                'lastName' => "Degrasse Tyson"
            )
        )

    )
); 

Result

If:
<div class="entry">
    <h1>Charles Jolley</h1>
</div>

Else:
<div class="entry"></div>

If with a loop:
<div class="entry">
    <h1>Nikola Tesla</h1>
    <h1>Carl Segan</h1>
    <h1>Neil Degrasse Tyson</h1>
</div>
joeybaker commented 11 years ago

{{^ var }} is also causing an issue:

Unexpected closing tag: /var
/Handlebars/Parser.php at: 72
fzerorubigd commented 11 years ago

@joshpangell thanks for your report. @joeybaker its re-implemented now in c735de8e5b0ae8d99e977fb3f89f686b5757d089

joeybaker commented 11 years ago

@fzerorubigd thanks!

FWIW, I'm looking at replacing our use the the mustache lib, with this one in production. Since there are no open issues (woot!) is there anything that needs working on?

fzerorubigd commented 11 years ago

@joeybaker nothing, except for tests. we use this in our project, and for now I'm busy with another part. as soon as I can, I want to write tests and after that anything is ok :)

joshpangell commented 11 years ago

Thanks for all the help and work on this @fzerorubigd - I have integrated this, and credited your library, here: http://cargocollective.com/#/opensource

joeybaker commented 11 years ago

@fzerorubigd thanks!