DamianEdwards / RazorSlices

Lightweight Razor-based templates for ASP.NET Core without MVC, Razor Pages, or Blazor.
MIT License
297 stars 12 forks source link

Add support for rendering slices to a StringBuilder and more #25

Closed madeyoga closed 1 year ago

madeyoga commented 1 year ago

I have made the following changes to the code:

I named the static class RazorSliceStringBuilderExtensions.

Usage

Render to a StringBuilder

RazorSlice slice = RazorSlice.Create("/Slices/LoremStatic.cshtml");
var sb = new StringBuilder();
await slice.RenderAsync(sb);
var template = sb.ToString();

Render to a string

RazorSlice slice = RazorSlice.Create("/Slices/LoremStatic.cshtml");
string template = await slice.RenderAsync();

I believe that with the introduction of these extensions, we will need to make the process of creating a slice easier, particularly for slices that have dependency-injected properties.

Currently, there is no overload that accepts both a string parameter for the slice name and an IServiceProvider. We could consider adding the following methods to address this:

public static RazorSlice Create(string sliceName, IServiceProvider serviceProvider);
public static RazorSlice<TModel> Create<TModel>(string sliceName, TModel model, IServiceProvider serviceProvider);

These methods incur a lookup cost each time they're invoked, which may affect performance. Given this, should we still consider adding them?

DamianEdwards commented 1 year ago

Currently, there is no overload that accepts both a string parameter for the slice name and an IServiceProvider. We could consider adding the following methods to address this:

That's a good idea. If you want to do that as part of this PR go ahead, or just log an issue with the details and we can do it separately.