StubbleOrg / Stubble

Trimmed down {{mustache}} templates in .NET
Other
399 stars 58 forks source link

Render pipeline is not passed to partials #126

Closed EndOfTheSpline closed 2 years ago

EndOfTheSpline commented 2 years ago

While using Stubble.Helpers, I've encountered an issue that helpers are not working when used in a partial. I've traced the issue down to the PartialTokenRenderer, in particular this line:

https://github.com/StubbleOrg/Stubble/blob/4958ef8365d55d256df7169967dc1869337aab81/src/Stubble.Core/Renderers/StringRenderer/TokenRenderers/PartialTokenRenderer.cs#L45

Because the pipeline settings of the parent context are not passed to Parse (i.e. it's missing , pipeline: context.RendererSettings.ParserPipeline), partials are always rendered with a default pipeline, even if their parent had pipeline modifications.

Workaround

As this library seems a bit dead in terms of PRs/updates, a workaround is to copy the PartialTokenRenderer, then "simply" replacing the built-in renderer:

var renderer = new StubbleBuilder()
    .Configure(builder =>
    {
        var tokenRenderers = builder.TokenRenderers;
        var idx = tokenRenderers.FindIndex(p => p.GetType().FullName == "Stubble.Core.Renderers.StringRenderer.TokenRenderers.PartialTokenRenderer");
        tokenRenderers.RemoveAt(idx);
        tokenRenderers.Insert(idx, new FixedPartialTokenRenderer());
    })
    .Build();

While not exactly pretty, it gets the job done, and allows partials to use the same render pipeline that was configured for the original renderer.

Romanx commented 2 years ago

Hey @EndOfTheSpline! Thanks for your patience this has now been released to nuget as v1.10.8

Thanks for using stubble and letting us know about this