diff --git a/DocxTemplater.Test/CollectionInTableTest.cs b/DocxTemplater.Test/CollectionInTableTest.cs new file mode 100644 index 0000000..329b6f5 --- /dev/null +++ b/DocxTemplater.Test/CollectionInTableTest.cs @@ -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; } + } + } +} \ No newline at end of file diff --git a/DocxTemplater.Test/DocxTemplater.Test.csproj b/DocxTemplater.Test/DocxTemplater.Test.csproj index 9db41ff..1fb93c9 100644 --- a/DocxTemplater.Test/DocxTemplater.Test.csproj +++ b/DocxTemplater.Test/DocxTemplater.Test.csproj @@ -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> diff --git a/DocxTemplater.Test/Resources/CollectionInTableCell.docx b/DocxTemplater.Test/Resources/CollectionInTableCell.docx new file mode 100644 index 0000000..214aeeb Binary files /dev/null and b/DocxTemplater.Test/Resources/CollectionInTableCell.docx differ diff --git a/DocxTemplater/Blocks/CollectionSeparatorBlock.cs b/DocxTemplater/Blocks/CollectionSeparatorBlock.cs index f0635cd..a5d0872 100644 --- a/DocxTemplater/Blocks/CollectionSeparatorBlock.cs +++ b/DocxTemplater/Blocks/CollectionSeparatorBlock.cs @@ -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) {