Skip to content

Commit

Permalink
Merge pull request #284 from jafin/feat/fluid-templateoptions
Browse files Browse the repository at this point in the history
feat(fluid) Adds templateOptions as a optional parameter
  • Loading branch information
lukencode authored Mar 21, 2022
2 parents 7e06647 + e10a74b commit ce857a8
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Renderers/FluentEmail.Liquid/LiquidRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public async Task<string> ParseAsync<T>(string template, T model, bool isHtml =
var fileProvider = rendererOptions.FileProvider;
var viewTemplate = ParseTemplate(template);

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

return await layoutTemplate.RenderAsync(context, rendererOptions.TextEncoder);
}
Expand Down
5 changes: 5 additions & 0 deletions src/Renderers/FluentEmail.Liquid/LiquidRendererOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,10 @@ public class LiquidRendererOptions
/// File provider to use, used when resolving references in templates, like master layout.
/// </summary>
public IFileProvider? FileProvider { get; set; }

/// <summary>
/// Set custom Template Options for Fluid
/// </summary>
public TemplateOptions TemplateOptions { get; set; } = new();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
using System;
using System.Collections.Generic;
using FluentAssertions;
using FluentEmail.Core;
using Fluid;
using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Options;
using NUnit.Framework;

namespace FluentEmail.Liquid.Tests.ComplexModel
{
public class ComplexModelRenderTests
{
public ComplexModelRenderTests()
{
SetupRenderer();
}

[Test]
public void Can_Render_Complex_Model_Properties()
{
var model = new ParentModel
{
ParentName = new NameDetails { Firstname = "Luke", Surname = "Dinosaur" },
ChildrenNames = new List<NameDetails>
{
new NameDetails { Firstname = "ChildFirstA", Surname = "ChildLastA" },
new NameDetails { Firstname = "ChildFirstB", Surname = "ChildLastB" }
}
};

var expected = @"
Parent: Luke
Children:
* ChildFirstA ChildLastA
* ChildFirstB ChildLastB
";

var email = Email
.From(TestData.FromEmail)
.To(TestData.ToEmail)
.Subject(TestData.Subject)
.UsingTemplate(Template(), model);
email.Data.Body.Should().Be(expected);
}

private string Template()
{
return @"
Parent: {{ ParentName.Firstname }}
Children:
{% for Child in ChildrenNames %}
* {{ Child.Firstname }} {{ Child.Surname }}{% endfor %}
";
}

private static void SetupRenderer(
IFileProvider fileProvider = null,
Action<TemplateContext, object> configureTemplateContext = null)
{
var options = new LiquidRendererOptions
{
FileProvider = fileProvider,
ConfigureTemplateContext = configureTemplateContext,
TemplateOptions = new TemplateOptions { MemberAccessStrategy = new UnsafeMemberAccessStrategy() }
};
Email.DefaultRenderer = new LiquidRenderer(Options.Create(options));
}
}
}
17 changes: 17 additions & 0 deletions test/FluentEmail.Liquid.Tests/ComplexModel/ParentModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Collections.Generic;

namespace FluentEmail.Liquid.Tests
{
public class ParentModel
{
public string Id { get; set; }
public NameDetails ParentName { get; set; }
public List<NameDetails> ChildrenNames { get; set; } = new List<NameDetails>();
}

public class NameDetails
{
public string Firstname { get; set; }
public string Surname { get; set; }
}
}
9 changes: 9 additions & 0 deletions test/FluentEmail.Liquid.Tests/ComplexModel/TestData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace FluentEmail.Liquid.Tests
{
public static class TestData
{
public const string ToEmail = "bob@test.com";
public const string FromEmail = "johno@test.com";
public const string Subject = "sup dawg";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@


<ItemGroup>
<PackageReference Include="FluentAssertions" Version="6.1.0" />
<PackageReference Include="Microsoft.Extensions.FileProviders.Embedded" Version="5.0.2" />
<PackageReference Include="Microsoft.Extensions.FileProviders.Physical" Version="5.0.0" />
</ItemGroup>
Expand Down

0 comments on commit ce857a8

Please sign in to comment.