Skip to content

Commit

Permalink
Merge pull request #4 from Amberg/workitems/AllowUnderscore
Browse files Browse the repository at this point in the history
Fix: Allow underscore in placeholder
  • Loading branch information
Amberg authored Jan 18, 2024
2 parents ad95498 + abdc9b3 commit c5e738a
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
4 changes: 4 additions & 0 deletions DocxTemplater.Test/PatternMatcherTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,16 @@ static IEnumerable TestPatternMatch_Cases()

yield return new TestCaseData("{{/Items}}").Returns(new[] { PatternType.LoopEnd });
yield return new TestCaseData("{{#items}}").Returns(new[] { PatternType.LoopStart });
yield return new TestCaseData("{{#ds.items_foo}}").Returns(new[] { PatternType.LoopStart }).SetName("LoopStart Underscore dots");
yield return new TestCaseData("{{/ds.items_foo}}").Returns(new[] { PatternType.LoopEnd }).SetName("LoopEnd Underscore dots");
yield return new TestCaseData("{{/Items.InnerCollection}}").Returns(new[] { PatternType.LoopEnd });
yield return new TestCaseData("{{#items.InnerCollection}}").Returns(new[] { PatternType.LoopStart });
yield return new TestCaseData("{{a.foo > 5}}").Returns(new[] { PatternType.Condition });
yield return new TestCaseData("{{ a > 5 }}").Returns(new[] { PatternType.Condition });
yield return new TestCaseData("{{ a / 20 >= 12 }}").Returns(new[] { PatternType.Condition });
yield return new TestCaseData("{{var}:F(d)}").Returns(new[] { PatternType.Variable });
yield return new TestCaseData("{{ds.foo.var}:F(d)}").Returns(new[] { PatternType.Variable }).SetName("Variable with dot");
yield return new TestCaseData("{{ds.foo_blubb.var}:F(d)}").Returns(new[] { PatternType.Variable }).SetName("Variable with underscore");
yield return new TestCaseData("{{var}:toupper}").Returns(new[] { PatternType.Variable });
yield return new TestCaseData("{{else}}").Returns(new[] { PatternType.ConditionElse });
yield return new TestCaseData("{{var}:format(a,b)}").Returns(new[] { PatternType.Variable })
Expand Down
2 changes: 1 addition & 1 deletion DocxTemplater/PatterMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ internal static class PatternMatcher
(?:
(?<prefix>[\/\#])? #prefix
(?:
(?<varname>[a-zA-Z0-9\.]+) #variable name
(?<varname>[a-zA-Z0-9\._]+) #variable name
| #or
(?<condition>[a-zA-Z0-9+\-*\/><=\s\.]+) #condition
)?
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ This can be used, for example, to bind a collection to a table. In this case, th

| Name | Position |
|----------------|----------|
| **{{#Items}}** {{Items.Name}} | {{Items.Position}} **{{/Items}}*|
| **{{#Items}}** {{Items.Name}} | {{Items.Position}} **{{/Items}}**|

This template bound to a model:
```c#
Expand Down Expand Up @@ -114,7 +114,7 @@ All document content between the start and end tag is rendered only if the condi

```
{{Item.Value >= 0}}Only visible if value is >= 0 {{/}}
{{Item.Value < 0}}Otherwise this text is shown{{/}}
{{else}}Otherwise this text is shown{{/}}
```

## Formatters
Expand Down

0 comments on commit c5e738a

Please sign in to comment.