Skip to content
This repository has been archived by the owner on Jan 26, 2019. It is now read-only.

Support for @first and @last in #if helper #52

Closed
aendra-rininsland opened this issue Feb 25, 2014 · 8 comments
Closed

Support for @first and @last in #if helper #52

aendra-rininsland opened this issue Feb 25, 2014 · 8 comments

Comments

@aendra-rininsland
Copy link

Apparently @FIRST and @last work in the #if helper now, but not in handlebars.php. See: handlebars-lang/handlebars.js#483

You can get around this somewhat using @index due type coercion:

{{#unless @index}}This is the first element{{/unless}}
{{#if @index}}This is not the first element{{/if}}

...But really, these should be implemented to maintain consistency with Handlebars syntax.

I may get some time to work on this today and will issue a pull request if so.

@everplays
Copy link
Contributor

Thanks but before any implementation please have a look at #51. Probably it
addresses what you need.
On Feb 25, 2014 8:08 PM, "Ændrew Rininsland" [email protected]
wrote:

Apparently @FIRST https://github.com/first and @lasthttps://github.com/lastwork in the #if helper now, but not in handlebars.php. See:
handlebars-lang/handlebars.js#483handlebars-lang/handlebars.js#483

You can get around this somewhat using @index due type coercion:

{{#unless @index}}This is the first element{{/unless}}
{{#if @index}}This is not the first element{{/if}}

...But really, these should be implemented to maintain consistency with
Handlebars syntax.

I may get some time to work on this today and will issue a pull request if
so.


Reply to this email directly or view it on GitHubhttps://github.com//issues/52
.

@aendra-rininsland
Copy link
Author

Hi!
Didn't get around to it, though am looking at that pull request now. I'd essentially implement the same logic used with the #each helper on the #if helper, seems pretty straightforward.

@everplays
Copy link
Contributor

I'm not sure if I get what you need. please have a look at the following example:

$h = new Handlebars\Handlebars;

echo $h->render(
    '{{#each data}}
    {{@index}} {{#unless @last}}Not last one!{{/unless}}{{#if @last}}Last entry!{{/if}}
{{/each}}',
    array(
        'data' => ['a', 'b', 'c']
    )
);

echo "\n";

echo $h->render(
    '{{#each data}}
    {{@index}} {{#if @first}}The first!{{/if}}{{#unless @first}}Not first!{{/unless}}
{{/each}}',
    array(
        'data' => ['a', 'b', 'c']
    )
);

echo "\n";

echo $h->render(
    '{{#each data}}
    {{@index}} {{#unless @index}}The first!{{/unless}}{{#if @index}}Not first!{{/if}}
{{/each}}',
    array(
        'data' => ['a', 'b', 'c']
    )
);

the output (master) will be:

    0 Not last one!
    1 Not last one!
    2 Last entry!

    0 The first!
    1 Not first!
    2 Not first!

    0 The first!
    1 Not first!
    2 Not first!

which is what you're looking for, right? even the example in handlebars-lang/handlebars.js#483, works:

$h = new Handlebars\Handlebars;

echo $h->render(
    '
{{#each data}}
    {{@index}} 
   {{#if @last }}
       Last entry!
    {{/if}}
{{/each}}',
    array(
        'data' => ['a', 'b', 'c']
    )
);

the output:

    0 
    1 
    2 
       Last entry!

@aendra-rininsland
Copy link
Author

Ah! Apologies, I hadn't seen those tests. Yes, you are correct — it should already be working as such.

Which is bizarre; I get the following error using the current "dev-master" version, installed via Composer and using PHP 5.3.10:

Fatal error: Uncaught exception 'InvalidArgumentException' with message 'variable name is invalid' in /home/aendrew/workspace/lf/wordpress/wp-content/plugins/longform/vendor/xamin/handlebars.php/src/Handlebars/Context.php:309
Stack trace:
#0 /home/aendrew/workspace/lf/wordpress/wp-content/plugins/longform/vendor/xamin/handlebars.php/src/Handlebars/Context.php(244): Handlebars\Context->_splitVariableName('@first')
#1 /home/aendrew/workspace/lf/wordpress/wp-content/plugins/longform/vendor/xamin/handlebars.php/src/Handlebars/Helpers.php(91): Handlebars\Context->get('@first')
#2 [internal function]: Handlebars\{closure}(Object(Handlebars\Template), Object(Handlebars\Context), '@first', 'first!')
#3 /home/aendrew/workspace/lf/wordpress/wp-content/plugins/longform/vendor/xamin/handlebars.php/src/Handlebars/Template.php(276): call_user_func_array(Object(Closure), Array)
#4 /home/aendrew/workspace/lf/wordpress/wp-content/plugins/longform/vendor/xamin/handlebars.php/src/Handlebars/Template.php(158): Handlebars\Template->_section(O in /home/aendrew/workspace/lf/wordpress/wp-content/plugins/longform/vendor/xamin/handlebars.php/src/Handlebars/Context.php on line 309 (edited)

Using this template:

{{#each slides}}
  {{#if @first}}first!{{/if}}
{{/each}}

However, this works:

{{#each slides}}
  {{@index}}
{{/each}}

Output: 123

I also ran the included tests without issue:

aendrew@devbox:~/workspace/longform/vendor/xamin/handlebars.php$ phpunit --bootstrap tests/bootstrap.php -v
PHPUnit 3.7.29 by Sebastian Bergmann.

Configuration read from /home/aendrew/workspace/longform/vendor/xamin/handlebars.php/phpunit.xml.dist

...........................................................

Time: 45 ms, Memory: 4.00Mb

OK (59 tests, 118 assertions)

Any idea what's wrong?

@JustBlackBird
Copy link
Contributor

@Aendrew, you've missed slash in the closing "if" tag. The correct template is:

{{#each slides}}
  {{#if @first}}first!{{/if}}
{{/each}}

@aendra-rininsland
Copy link
Author

@JustBlackBird -- Apologies, copying text between two keyboards I missed that. It was correct in my code, though; even after copypasting your corrected template into my code I get the same error.

Also, "slides" is definitely an indexed array FWIW.

@everplays
Copy link
Contributor

@Aendrew are you sure that your handlebars.php installation is uptodate? because there's no exception on line 309 of Context.php, have a look for yourself:

@aendra-rininsland
Copy link
Author

@everplays Ah, good catch! Looking at composer.lock, it seems I was at commit 5b188ce, which is just a bit before @JustBlackBird's work above.

Doing php composer.phar self-update; php composer.phar update has brought me to the latest version of everything and using @first/@last with #if is now working. Which is super weird, because I only installed it yesterday. Hm.

Okay everyone, sorry for all the confusion and thanks for the help!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants