Skip to content

Commit ce857a8

Browse files
authored
Merge pull request #284 from jafin/feat/fluid-templateoptions
feat(fluid) Adds templateOptions as a optional parameter
2 parents 7e06647 + e10a74b commit ce857a8

File tree

6 files changed

+105
-2
lines changed

6 files changed

+105
-2
lines changed

src/Renderers/FluentEmail.Liquid/LiquidRenderer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public async Task<string> ParseAsync<T>(string template, T model, bool isHtml =
3434
var fileProvider = rendererOptions.FileProvider;
3535
var viewTemplate = ParseTemplate(template);
3636

37-
var context = new TemplateContext(model)
37+
var context = new TemplateContext(model, rendererOptions.TemplateOptions)
3838
{
3939
// provide some services to all statements
4040
AmbientValues =
@@ -56,7 +56,7 @@ public async Task<string> ParseAsync<T>(string template, T model, bool isHtml =
5656
if (context.AmbientValues.TryGetValue("Layout", out var layoutPath))
5757
{
5858
context.AmbientValues["Body"] = body;
59-
var layoutTemplate = ParseLiquidFile((string) layoutPath, fileProvider!);
59+
var layoutTemplate = ParseLiquidFile((string)layoutPath, fileProvider!);
6060

6161
return await layoutTemplate.RenderAsync(context, rendererOptions.TextEncoder);
6262
}

src/Renderers/FluentEmail.Liquid/LiquidRendererOptions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,10 @@ public class LiquidRendererOptions
2525
/// File provider to use, used when resolving references in templates, like master layout.
2626
/// </summary>
2727
public IFileProvider? FileProvider { get; set; }
28+
29+
/// <summary>
30+
/// Set custom Template Options for Fluid
31+
/// </summary>
32+
public TemplateOptions TemplateOptions { get; set; } = new();
2833
}
2934
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using FluentAssertions;
4+
using FluentEmail.Core;
5+
using Fluid;
6+
using Microsoft.Extensions.FileProviders;
7+
using Microsoft.Extensions.Options;
8+
using NUnit.Framework;
9+
10+
namespace FluentEmail.Liquid.Tests.ComplexModel
11+
{
12+
public class ComplexModelRenderTests
13+
{
14+
public ComplexModelRenderTests()
15+
{
16+
SetupRenderer();
17+
}
18+
19+
[Test]
20+
public void Can_Render_Complex_Model_Properties()
21+
{
22+
var model = new ParentModel
23+
{
24+
ParentName = new NameDetails { Firstname = "Luke", Surname = "Dinosaur" },
25+
ChildrenNames = new List<NameDetails>
26+
{
27+
new NameDetails { Firstname = "ChildFirstA", Surname = "ChildLastA" },
28+
new NameDetails { Firstname = "ChildFirstB", Surname = "ChildLastB" }
29+
}
30+
};
31+
32+
var expected = @"
33+
Parent: Luke
34+
Children:
35+
36+
* ChildFirstA ChildLastA
37+
* ChildFirstB ChildLastB
38+
";
39+
40+
var email = Email
41+
.From(TestData.FromEmail)
42+
.To(TestData.ToEmail)
43+
.Subject(TestData.Subject)
44+
.UsingTemplate(Template(), model);
45+
email.Data.Body.Should().Be(expected);
46+
}
47+
48+
private string Template()
49+
{
50+
return @"
51+
Parent: {{ ParentName.Firstname }}
52+
Children:
53+
{% for Child in ChildrenNames %}
54+
* {{ Child.Firstname }} {{ Child.Surname }}{% endfor %}
55+
";
56+
}
57+
58+
private static void SetupRenderer(
59+
IFileProvider fileProvider = null,
60+
Action<TemplateContext, object> configureTemplateContext = null)
61+
{
62+
var options = new LiquidRendererOptions
63+
{
64+
FileProvider = fileProvider,
65+
ConfigureTemplateContext = configureTemplateContext,
66+
TemplateOptions = new TemplateOptions { MemberAccessStrategy = new UnsafeMemberAccessStrategy() }
67+
};
68+
Email.DefaultRenderer = new LiquidRenderer(Options.Create(options));
69+
}
70+
}
71+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System.Collections.Generic;
2+
3+
namespace FluentEmail.Liquid.Tests
4+
{
5+
public class ParentModel
6+
{
7+
public string Id { get; set; }
8+
public NameDetails ParentName { get; set; }
9+
public List<NameDetails> ChildrenNames { get; set; } = new List<NameDetails>();
10+
}
11+
12+
public class NameDetails
13+
{
14+
public string Firstname { get; set; }
15+
public string Surname { get; set; }
16+
}
17+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace FluentEmail.Liquid.Tests
2+
{
3+
public static class TestData
4+
{
5+
public const string ToEmail = "bob@test.com";
6+
public const string FromEmail = "johno@test.com";
7+
public const string Subject = "sup dawg";
8+
}
9+
}

test/FluentEmail.Liquid.Tests/FluentEmail.Liquid.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222

2323
<ItemGroup>
24+
<PackageReference Include="FluentAssertions" Version="6.1.0" />
2425
<PackageReference Include="Microsoft.Extensions.FileProviders.Embedded" Version="5.0.2" />
2526
<PackageReference Include="Microsoft.Extensions.FileProviders.Physical" Version="5.0.0" />
2627
</ItemGroup>

0 commit comments

Comments
 (0)