jehugaleahsa / mustache-sharp

An extension of the mustache text template engine for .NET.
The Unlicense
306 stars 78 forks source link

Feature Request - {{@index}} #8

Closed sdeller closed 11 years ago

sdeller commented 11 years ago

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 commented 11 years ago

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/jehugaleahsa/mustache-sharp/issues/8 .

jehugaleahsa commented 11 years ago

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

sdeller commented 11 years ago

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 commented 11 years ago

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/jehugaleahsa/mustache-sharp/issues/8#issuecomment-21383073 .

sdeller commented 11 years ago

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 commented 11 years ago

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/jehugaleahsa/mustache-sharp/issues/8#issuecomment-21904734 .