Skip to content

Commit

Permalink
Fix bug in Nested CollectionSeparatorBlock
Browse files Browse the repository at this point in the history
  • Loading branch information
Amberg committed Oct 3, 2024
1 parent aacd98e commit 1714ee1
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
49 changes: 49 additions & 0 deletions DocxTemplater.Test/CollectionInTableTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
namespace DocxTemplater.Test
{
internal class CollectionInTableTest
{
[Test]
public void CollectionInTableTestRender()
{

using var fileStream = File.OpenRead("Resources/CollectionInTableCell.docx");
var docTemplate = new DocxTemplate(fileStream);
var data = new MyLessonsReportModel();
var lesson1 = new LessonReportModel
{
Date = DateTime.Now,
CourseDisplayName = "Course 1",
ParticipantsCount = 10,
Resources = new List<string> { "Resource 1", "Resource 2" }
};
var lesson2 = new LessonReportModel
{
Date = DateTime.Now.AddDays(1),
CourseDisplayName = "Course 2",
ParticipantsCount = 20,
Resources = new List<string> { "Resource 3", "Resource 4" }
};
data.Lessons = new List<LessonReportModel> { lesson1 };
docTemplate.BindModel("ds", data);
var result = docTemplate.Process();
docTemplate.Validate();
result.SaveAsFileAndOpenInWord();
}

public class MyLessonsReportModel
{
public IReadOnlyCollection<LessonReportModel> Lessons { get; set; } = new List<LessonReportModel>();
}

public class LessonReportModel
{
public DateTime Date { get; set; }

public string CourseDisplayName { get; set; }

public ICollection<string> Resources { get; set; } = new List<string>();

public int ParticipantsCount { get; set; }
}
}
}
3 changes: 3 additions & 0 deletions DocxTemplater.Test/DocxTemplater.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
<None Update="Resources\BillTemplate2.docx">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Resources\CollectionInTableCell.docx">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Resources\ComplexTemplate.docx">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
Expand Down
Binary file not shown.
4 changes: 2 additions & 2 deletions DocxTemplater/Blocks/CollectionSeparatorBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public CollectionSeparatorBlock(IVariableReplacer variableReplacer, PatternType

public override void Expand(IModelLookup models, OpenXmlElement parentNode)
{
int count = (int)models.GetValue($"{ParentBlock.StartMatch.Variable}._Idx");
int length = (int)models.GetValue($"{ParentBlock.StartMatch.Variable}._Length");
int count = (int)models.GetValue($"{ParentBlock.StartMatch.Variable.TrimStart('.')}._Idx");
int length = (int)models.GetValue($"{ParentBlock.StartMatch.Variable.TrimStart('.')}._Length");
// last element is rendered first - get length and count ot to not render the last separator
if (length - count == 0)
{
Expand Down

0 comments on commit 1714ee1

Please sign in to comment.