-
Notifications
You must be signed in to change notification settings - Fork 132
Support for @first and @last in #if helper #52
Comments
Thanks but before any implementation please have a look at #51. Probably it
|
Hi! |
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:
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:
|
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:
Using this template:
However, this works:
Output: I also ran the included tests without issue:
Any idea what's wrong? |
@Aendrew, you've missed slash in the closing "if" tag. The correct template is:
|
@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. |
@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: handlebars.php/src/Handlebars/Context.php Line 309 in 3514a2b |
@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 Okay everyone, sorry for all the confusion and thanks for the help! |
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.
The text was updated successfully, but these errors were encountered: