Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request - {{@index}} #8

Closed
sdeller opened this issue Jul 18, 2013 · 6 comments
Closed

Feature Request - {{@index}} #8

sdeller opened this issue Jul 18, 2013 · 6 comments

Comments

@sdeller
Copy link

sdeller commented Jul 18, 2013

One of the things we used frequently in front-end handlebars.js implementations was the item index over array loops to help with various jquery/css sliders, etc. I don't see direct support for the {{@Index}} var in the current mustache# build. is there a mechanism to reference the loop index in an "each" construct, or are there plans for future support?

@jehugaleahsa
Copy link
Owner

I can see that being a useful feature. I wasn't even aware of it. I will
think about how to codify it.

On Thu, Jul 18, 2013 at 2:16 PM, sdeller notifications@github.com wrote:

One of the things we used frequently in front-end handlebars.js
implementations was the item index over array loops to help with various
jquery/css sliders, etc. I don't see direct support for the {{@indexhttps://github.com/index}}
var it in the current mustache# build. is there a mechanism to reference
the loop index in an "each" construct, or are there plans for future
support?


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

@jehugaleahsa
Copy link
Owner

This feature is now available with 0.1.2.0. Instead of @index, it is #index.

@sdeller
Copy link
Author

sdeller commented Jul 22, 2013

Thanks Travis! Kicking the tires on it, and ran into a minor glitch

This works fine

   {{#each this}}
            Item Number: {{#index}} <br>
   {{/each}}

This works fine

   {{#each this}}
            Item Number: foo<br>
   {{/each}}
   {{#each this}}
            Item Number: {{#index}} <br>
   {{/each}}

This works fine

   {{#each this}}
          Item Number: {{#index}} {{SomeValue}}<br>
  {{/each}}
   {{#each this}}
            Item Number: {{#index}} {{AnotherValue}}<br>
   {{/each}}

This throws an exception: "The wrong number of arguments were passed to an index tag."

   {{#each this}}
              Item Number: {{#index}} <br>
  {{/each}}
  {{#each this}}
            Item Number: foo<br>
  {{/each}}

It seems that if you have 2 each loops over the same list/array, and the first loop has a {{#index}} and does not at least have 1 reference to an object property, it throws the exception. The second loop does not have a problem, even if it doesn't have a property reference in it

For reference, here is how I am passing the template and object in.

            Mustache.FormatCompiler mCompiler = new Mustache.FormatCompiler();
            Mustache.Generator generator = mCompiler.Compile(template);
            result = generator.Render(objectSource);

and the test object definition

List objectSource = new List();
objectSource.Add(new TestObject { Name = "name1", Val = "val1" });
objectSource.Add(new TestObject { Name = "name2", Val = "val2" });
objectSource.Add(new TestObject { Name = "name3", Val = "val3" });

public class TestObject
{
public String Name { get; set; }
public String Val { get; set; }
}

@jehugaleahsa
Copy link
Owner

I think I fixed my issue. Please get the latest version and run your tests
again.
#index is really the first in-line tag in mustache#. It was consuming too
many characters from the input stream.
The error was caused by #index trying to grab arguments from the next #each
tag.

Your example made me realize a subtle inconsistency in the way mustache#
handles newlines.
It's made me think about eliminating the special whitespace rules. The
rules are complicated and obscure.
It's easier to eliminate extraneous whitespace simply by moving text next
to the starting tag.

On Mon, Jul 22, 2013 at 7:18 PM, sdeller notifications@github.com wrote:

Thanks Travis! Kicking the tires on it, and ran into a minor glitch

This works fine

{{#each this}}
Item Number: {{#index}}

{{/each}}

This works fine

{{#each this}}
Item Number: foo

{{/each}}
{{#each this}}
Item Number: {{#index}}

{{/each}}

This works fine

{{#each this}}
Item Number: {{#index}} {{SomeValue}}

{{/each}}
{{#each this}}
Item Number: {{#index}} {{AnotherValue}}

{{/each}}

This throws an exception: "The wrong number of arguments were passed to an
index tag."

{{#each this}}
Item Number: {{#index}}

{{/each}}
{{#each this}}
Item Number: foo

{{/each}}

It seems that if you have 2 each loops over the same list/array, and the
first loop has a {{#index}} and does not at least have 1 reference to an
object property, it throws the exception. The second loop does not have a
problem, even if it doesn't have a property reference in it

For reference, here is how I am passing the template and object in.

        Mustache.FormatCompiler mCompiler = new Mustache.FormatCompiler();
        Mustache.Generator generator = mCompiler.Compile(template);
        result = generator.Render(objectSource);

and the test object definition

List objectSource = new List();
objectSource.Add(new TestObject { Name = "name1", Val = "val1" });
objectSource.Add(new TestObject { Name = "name2", Val = "val2" });
objectSource.Add(new TestObject { Name = "name3", Val = "val3" });

public class TestObject
{
public String Name { get; set; }
public String Val { get; set; }
}


Reply to this email directly or view it on GitHubhttps://github.com//issues/8#issuecomment-21383073
.

@sdeller
Copy link
Author

sdeller commented Jul 31, 2013

any chance the shiny new #index parameter will be accessible from the #if conditional?

ex:

{{#each this}}
{{#if index}}
First !
{{#else}}
Not First
{{/if}}
{{/each}}

@jehugaleahsa
Copy link
Owner

Not yet...
On Jul 31, 2013 7:45 PM, "sdeller" notifications@github.com wrote:

any chance the shiny new #index parameter will be accessible from the #if
conditional?

ex:

{{#each this}}
{{#if index}}
First !
{{#else}}
Not First
{{/if}}
{{/each}}


Reply to this email directly or view it on GitHubhttps://github.com//issues/8#issuecomment-21904734
.

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

No branches or pull requests

2 participants