Skip to content

Commit

Permalink
feat: Support for parameters in partials
Browse files Browse the repository at this point in the history
  • Loading branch information
Antaris committed Nov 1, 2024
1 parent b900023 commit 9e40ccd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
8 changes: 8 additions & 0 deletions libs/FuManchu/Renderer/PartialBlockRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace FuManchu.Renderer;

using System.Collections.Generic;
using System.Dynamic;
using System.IO;
using System.Linq;

Expand Down Expand Up @@ -40,6 +41,13 @@ protected override void Render(Block block, Arguments? arguments, Map? maps, Ren
Write(scope.ScopeContext, writer, new SafeString(context.Service.RunPartial(name, scope.ScopeContext)));
}
}
else if (maps is { Count: > 0 })
{
using (var scope = context.BeginScope(maps))
{
Write(scope.ScopeContext, writer, new SafeString(context.Service.RunPartial(name, scope.ScopeContext)));
}
}
else
{
Write(context, writer, new SafeString(context.Service.RunPartial(name, context)));
Expand Down
11 changes: 11 additions & 0 deletions tests/FuManchu.Tests/Renderer/PartialBlockRendererFacts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,15 @@ public void CanRenderPartialWithChildModel()

RenderTest(template, expected, new { person = new { forename = "Matthew", surname = "Abbott" } });
}

[Fact]
public void CanRenderPartialWithArguments()
{
HandlebarsService.RegisterPartial("body", "{{firstName}} {{lastName}}");

string template = "{{>body firstName=person.forename lastName=person.surname}}";
string expected = "Matthew Abbott";

RenderTest(template, expected, new { person = new { forename = "Matthew", surname = "Abbott" } });
}
}

0 comments on commit 9e40ccd

Please sign in to comment.