Skip to content

@last element in nested each refers to the parent item ( C# ) #243

Closed
dotnet/arcade
#16884

Description

@ip75

I try to dump array with objects to csv file. Each object is a row with values.

{{#unless @last}};{{/unless}} block in nested loop is interpreted as a last parent element.

There is a code for LinqPad:

void Main()
{
	string template = "{{#each header}}{{this}}{{#unless @last}};{{/unless}}{{/each}}\n{{#each rows}}{{#each this}}{{this}}{{#unless @last}};{{/unless}}{{/each}}\n{{/each}}\n\n\n\n\n\n";			
			
			var data = new
            {
                header = new string [] { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" },
                rows = new item [] { new item(), new item(), new item(), new item(), new item(), new item(), new item() }
				
			};

	var handlebarsInstance = Handlebars.Create(new HandlebarsConfiguration {TextEncoder = new CsvEncoder()});
	Console.WriteLine(handlebarsInstance.Compile(template).Invoke(data));
}

public class CsvEncoder : ITextEncoder
{
    public string Encode(string value)
    {
        return value;
    }
}

public class item
{
	public string val1 {get; set;} = "A";
	public string val2 {get; set;} = "B";
	public string val3 {get; set;} = "C";
	public string val4 {get; set;} = "D";
	public string val5 {get; set;} = "E";
	public string val6 {get; set;} = "F";
	public string val7 {get; set;} = "G";
}

result is:

Sun;Mon;Tue;Wed;Thu;Fri;Sat
A;B;C;D;E;F;G;
A;B;C;D;E;F;G;
A;B;C;D;E;F;G;
A;B;C;D;E;F;G;
A;B;C;D;E;F;G;
A;B;C;D;E;F;G;
ABCDEFG

I expect this result:

Sun;Mon;Tue;Wed;Thu;Fri;Sat
A;B;C;D;E;F;G
A;B;C;D;E;F;G
A;B;C;D;E;F;G
A;B;C;D;E;F;G
A;B;C;D;E;F;G
A;B;C;D;E;F;G
A;B;C;D;E;F;G

Is this error in my handlebars template or bug in handlebars parser/composer for .net?

https://stackoverflow.com/questions/48424658/last-element-in-nested-each-handlebars-c-sharp

Activity

  1. changed the title [-]@last element in nested each (Handlebars) C#[/-] [+]@last element in nested each refer to parent item ( C# )[/+] on Jan 26, 2018
  2. changed the title [-]@last element in nested each refer to parent item ( C# )[/-] [+]@last element in nested each refers to the parent item ( C# )[/+] on Jan 26, 2018
  3. slahn commented on May 4, 2018

    @slahn

    In Handlebars.js, @last is not set when iterating an object. As the documentation says: "When iterating over an object only the @FIRST is available."

    If the data is an array (or list), this works as expected.

    var rowData = new [] {"A","B","C","D","E","F","G"};	
    var data = new
    {
    	header = new string[] { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" },
    	rows = Enumerable.Repeat(rowData, 7)
    }
    

    Maybe @last should be explicitly unset somehow, instead of having the parent value? So that it was always false? But that wouldn't solve the authors problem anyway.

  4. added a commit that references this issue on Dec 20, 2019
  5. added a commit that references this issue on Apr 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions