Skip to content

Commit

Permalink
feat: allow configuring Liquid parser (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
VilleHakli committed Nov 29, 2023
1 parent 4fa57c1 commit fb926fb
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Renderers/FluentEmail.Liquid/LiquidRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public LiquidRenderer(IOptions<LiquidRendererOptions> options)
{
_options = options;
_parser = new LiquidParser();
_options.Value.ConfigureParser?.Invoke(_parser);
}

public string Parse<T>(string template, T model, bool isHtml = true)
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 @@ -30,5 +30,10 @@ public class LiquidRendererOptions
/// Set custom Template Options for Fluid
/// </summary>
public TemplateOptions TemplateOptions { get; set; } = new TemplateOptions();

/// <summary>
/// Allows configuring parser
/// </summary>
public Action<LiquidParser>? ConfigureParser { get; set; }
}
}
34 changes: 33 additions & 1 deletion test/FluentEmail.Liquid.Tests/LiquidTests.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
using System;
using System.IO;
using System.Reflection;
using System.Text.Encodings.Web;
using System.Threading.Tasks;

using FluentEmail.Core;

using Fluid;
using Fluid.Ast;

using Microsoft.Extensions.FileProviders;
using Microsoft.Extensions.Options;
Expand All @@ -27,12 +31,14 @@ public void SetUp()

private static void SetupRenderer(
IFileProvider fileProvider = null,
Action<TemplateContext, object> configureTemplateContext = null)
Action<TemplateContext, object> configureTemplateContext = null,
Action<LiquidParser> configureParser = null)
{
var options = new LiquidRendererOptions
{
FileProvider = fileProvider,
ConfigureTemplateContext = configureTemplateContext,
ConfigureParser = configureParser
};
Email.DefaultRenderer = new LiquidRenderer(Options.Create(options));
}
Expand Down Expand Up @@ -181,6 +187,32 @@ public void Should_be_able_to_use_embedded_layout()
Assert.AreEqual($"<h2>Hello!</h2>{Environment.NewLine}<div>{Environment.NewLine}sup LUKE here is a list 123</div>", email.Data.Body);
}

[Test]
public void Should_be_able_to_configure_parser()
{
SetupRenderer(
new EmbeddedFileProvider(typeof(LiquidTests).Assembly, "FluentEmail.Liquid.Tests.EmailTemplates"),
configureParser: parser => parser.RegisterExpressionTag("testTag", TestTag)
);

const string template = "sup {{ Name }} here is a custom tag: {% testTag 'test' %}";

var email = Email
.From(FromEmail)
.To(ToEmail)
.Subject(Subject)
.UsingTemplate(template, new ViewModel { Name = "LUKE" });

Assert.AreEqual("sup LUKE here is a custom tag: Hello from custom tag test", email.Data.Body);

static async ValueTask<Completion> TestTag(Expression pathExpression, TextWriter writer, TextEncoder encoder, TemplateContext context)
{
var tagParameterValue = await pathExpression.EvaluateAsync(context);
await writer.WriteAsync($"Hello from custom tag {tagParameterValue.ToStringValue()}");
return Completion.Normal;
}
}

private class ViewModel
{
public string Name { get; set; }
Expand Down

0 comments on commit fb926fb

Please sign in to comment.