Document Solutions for Word
Report Templates / Template Configuration / Formatters / Sequence and Follow Formatter
In This Topic
    Sequence and Follow Formatter
    In This Topic

    Sequence and Follow formatter can be used to co-iterate the data from different datasources, meaning that the data from first datasource should contain only one iteration of data from the another datasource. To understand this better, lets assume we have two datasources:

    numbers = new[] { new { id = 1 }, new { id = 2 }, new { id = 3 }, new { id = 4 } }
    letters = new[] { new { id = "Apple" }, new { id = "Banana" }, new { id = "Orange" }, new { id = "Mango" } }

    After using a template layout and processing it, the output will look like: 

    Without Sequence and Follow Formatter With Sequence and Follow Formatter

    1 Apple

    1 Banana

    1 Orange

    1 Mango

    2 Apple

    2 Banana

    ...... so on

    1 Apple

    2 Banana

    3 Orange

    4 Mango

    The sequence and follow formatter can be used within a template range where a template tag starts with a '#' and ends with a '/'. The syntax of the formatters is:

    Here, the unique_id is an alias name which is bound to the template range containing the sequence formatter. The alias name should be unique across the template layout.

    Note: The sequence formatter can be written as 'sequence' or 'seq'.

    The usage of sequence and follow formatters should be done together to observe their intended behavior. The sequence formatter alone does not affect anything in the template range. However, when used with the follow formatter, it produces strictly one copy and data index of data in the follower collection. A usage example of both formatters in a template range:

    {{#numbers}:seq(num}{{numbers.id}}{{#letters}:follow(num)}{{letters.id}}{{/letters}}{{/numbers}}

    The following example shows how to use sequence and follow formatters to iterate only once over the data in two different datasources. The below image shows the template tags used inside the template. You can also download the Template layout here.

     The below code example loads the above template and processes it to save the final Word document:

    C#
    Copy Code
    var doc = new GcWordDocument();
    doc.Load("SeqFollowerFormatter.docx");
                
    var sequence = @"[
        { ""num"": ""First"" },
        { ""num"": ""Second"" },
        { ""num"": ""Third"" },
        { ""num"": ""Fourth"" },
        { ""num"": ""Fifth"" },
        { ""num"": ""Sixth"" },
        { ""num"": ""Seventh"" },
        { ""num"": ""Eights"" },
        { ""num"": ""Ninth"" },
        { ""num"": ""Tenth"" }
    ]";
    // Add data source
    doc.DataTemplate.DataSources.Add("nums", sequence);
    // Add second JSON data source
    var oceans = File.OpenRead("oceans.json");
    doc.DataTemplate.DataSources.Add("ds", oceans);
    
    doc.DataTemplate.Process();
    doc.Save("SeqFollowTemplate.docx");

     The output of above code example will look like below: 

    You can download the 'oceans.json' data source used in the above example by downloading the 'Sequence and Follow formatters' demo from here.

    Note: If the number of items in sequence and follow ranges are different, the template engine will stop when it reaches the end of the shorter of the two ranges. In other words, the number of repetitions will be the minimum of the two ranges' lengths.