Skip to content

Commit 4a1cfad

Browse files
committed
add support for if elase inside foreach
1 parent e4ab83f commit 4a1cfad

8 files changed

Lines changed: 296 additions & 174 deletions

File tree

README.md

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -227,13 +227,17 @@ MiniWord.SaveAsByTemplate(path, templatePath, value);
227227

228228
![before_foreach](https://user-images.githubusercontent.com/38832863/220123955-063c9345-3998-4fd7-982c-8d1e3b48bbf8.PNG)
229229

230+
<img width="755" alt="Screenshot 2023-08-08 at 17 59 37" src="https://github.com/mini-software/MiniWord/assets/38832863/7811bf53-48cf-4fa4-85d7-d98663feb119">
231+
230232
##### Result
231233

232234
![after_foreach](https://user-images.githubusercontent.com/38832863/220123960-913a7140-2fa2-415e-bb3e-456e04167382.PNG)
233235

236+
<img width="755" alt="Screenshot 2023-08-08 at 18 00 15" src="https://github.com/mini-software/MiniWord/assets/38832863/9e1afcf7-64b1-441c-8488-9ea2bd3114a1">
237+
234238
### If statement inside template
235239

236-
Adding `@if` and `@endif` tags to template is required.
240+
Adding `{{if` and `endif}}` tags to template is required.
237241

238242
##### Example
239243

@@ -260,13 +264,11 @@ MiniWord.SaveAsByTemplate(path, templatePath, value);
260264
```
261265

262266
##### Template
263-
264-
![before_if](https://user-images.githubusercontent.com/38832863/220125429-7dd6ce94-35c6-478e-8903-064f9cf9361a.PNG)
267+
<img width="931" alt="Screenshot 2023-08-08 at 17 55 46" src="https://github.com/mini-software/MiniWord/assets/38832863/2adea468-a9c1-422f-a270-167086bc4ba3">
265268

266269
##### Result
267270

268-
![after_if](https://user-images.githubusercontent.com/38832863/220125435-72ea24b4-2412-45de-961a-ad4b2134417b.PNG)
269-
271+
<img width="536" alt="Screenshot 2023-08-08 at 17 56 47" src="https://github.com/mini-software/MiniWord/assets/38832863/01f71c0f-eee0-4189-8510-abe063126514">
270272

271273
### ColorText
272274

release-note/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222

2323
---
2424

25+
### 0.7.1
26+
- [New] Add support to if statement inside foreach statement inside templates. Please refer to samples. (via @eynarhaji)
27+
- [Breaking change] Change tags for if statements from @if to {{if and @endif to endif}} inside templates. Please refer to samples. (via @eynarhaji)
2528

2629
### 0.7.0
2730
- [New] Add support to List inside List via `IEnumerable<MiniWordForeach>` and `{{foreach`/`endforeach}}` tags (via @eynarhaji)
50.4 KB
Binary file not shown.

samples/docx/TestIfStatement.docx

10 Bytes
Binary file not shown.

src/MiniWord/MiniWord.Implment.cs

Lines changed: 169 additions & 168 deletions
Large diffs are not rendered by default.

src/MiniWord/MiniWord.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<PropertyGroup>
33
<TargetFrameworks>net45;netstandard2.0;net5.0</TargetFrameworks>
44
<Version>0.7.0</Version>
5+
<Version>0.6.3</Version>
56
</PropertyGroup>
67
<PropertyGroup>
78
<AssemblyName>MiniWord</AssemblyName>

tests/MiniWordTests/MiniWordTests.cs

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ public void TestForeachLoopInTables()
118118
var xml = Helpers.GetZipFileContent(path, "word/document.xml");
119119
Assert.Contains(@"Discussion requirement part2 and development", xml);
120120
Assert.Contains(@"Discussion requirement part1", xml);
121+
Assert.Contains(
122+
"Air way to the Airplane | Parking way to the Car / Hotel way to the Room, Food way to the Plate", xml);
121123
}
122124

123125
[Fact]
@@ -152,5 +154,118 @@ public void MiniWordIfStatement_FirstIf()
152154
Assert.Contains("CreateDate is not less than 2021", docXml);
153155
Assert.DoesNotContain("CreateDate is not greater than 2021", docXml);
154156
}
157+
158+
[Fact]
159+
public void TestForeachLoopInTablesWithIfStatement()
160+
{
161+
var path = PathHelper.GetTempFilePath();
162+
var templatePath = PathHelper.GetFile("TestForeachInTablesWithIfStatementDemo.docx");
163+
var value = new Dictionary<string, object>()
164+
{
165+
["TripHs"] = new List<Dictionary<string, object>>
166+
{
167+
new Dictionary<string, object>
168+
{
169+
{ "sDate", DateTime.Parse("2022-09-08 08:30:00") },
170+
{ "eDate", DateTime.Parse("2022-09-08 15:00:00") },
171+
{ "How", "Discussion requirement part1" },
172+
{
173+
"Details", new List<MiniWordForeach>()
174+
{
175+
new MiniWordForeach()
176+
{
177+
Value = new Dictionary<string, object>()
178+
{
179+
{"Text", "Air"},
180+
{"Value", "Airplane"}
181+
},
182+
Separator = " | "
183+
},
184+
new MiniWordForeach()
185+
{
186+
Value = new Dictionary<string, object>()
187+
{
188+
{"Text", "Parking"},
189+
{"Value", "Car"}
190+
},
191+
Separator = " / "
192+
},
193+
new MiniWordForeach()
194+
{
195+
Value = new Dictionary<string, object>()
196+
{
197+
{"Text", "Hotel"},
198+
{"Value", "Room"}
199+
},
200+
Separator = ", "
201+
},
202+
new MiniWordForeach()
203+
{
204+
Value = new Dictionary<string, object>()
205+
{
206+
{"Text", "Food"},
207+
{"Value", "Plate"}
208+
},
209+
Separator = ""
210+
}
211+
}
212+
}
213+
},
214+
new Dictionary<string, object>
215+
{
216+
{ "sDate", DateTime.Parse("2022-09-09 08:30:00") },
217+
{ "eDate", DateTime.Parse("2022-09-09 17:00:00") },
218+
{ "How", "Discussion requirement part2 and development" },
219+
{
220+
"Details", new List<MiniWordForeach>()
221+
{
222+
new MiniWordForeach()
223+
{
224+
Value = new Dictionary<string, object>()
225+
{
226+
{"Text", "Air"},
227+
{"Value", "Airplane"}
228+
},
229+
Separator = " | "
230+
},
231+
new MiniWordForeach()
232+
{
233+
Value = new Dictionary<string, object>()
234+
{
235+
{"Text", "Parking"},
236+
{"Value", "Car"}
237+
},
238+
Separator = " / "
239+
},
240+
new MiniWordForeach()
241+
{
242+
Value = new Dictionary<string, object>()
243+
{
244+
{"Text", "Hotel"},
245+
{"Value", "Room"}
246+
},
247+
Separator = ", "
248+
},
249+
new MiniWordForeach()
250+
{
251+
Value = new Dictionary<string, object>()
252+
{
253+
{"Text", "Food"},
254+
{"Value", "Plate"}
255+
},
256+
Separator = ""
257+
}
258+
}
259+
}
260+
}
261+
}
262+
};
263+
MiniWord.SaveAsByTemplate(path, templatePath, value);
264+
//System.Diagnostics.Process.Start("explorer.exe", path);
265+
var xml = Helpers.GetZipFileContent(path, "word/document.xml");
266+
Assert.Contains(@"Discussion requirement part2 and development", xml);
267+
Assert.Contains(@"Discussion requirement part1", xml);
268+
Assert.Contains("Air way to the Airplane | Hotel way to the Room", xml);
269+
}
155270
}
156271
}

tests/MiniWordTests/MiniWordTests.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net5.0</TargetFrameworks>
4+
<TargetFrameworks>net6.0;</TargetFrameworks>
55

66
<IsPackable>false</IsPackable>
77
</PropertyGroup>

0 commit comments

Comments
 (0)