ltrzesniewski / RazorBlade

Compile Razor templates at build-time without a dependency on ASP.NET.
MIT License
149 stars 7 forks source link

Can't use template with a model when RazorBladeEmbeddedLibrary is true #4

Closed greghroberts closed 1 year ago

greghroberts commented 1 year ago

Hey, this package was exactly what I was looking for to add some simple templating capabilities to a CLI I'm working on for personal project.

I was trying out the RazorBladeEmbeddedLibrary mode described here to see if it would shrink my exe output and noticed it would only generate a default constructor even though the template uses @inherits RazorBlade.HtmlTemplate<MyModel>;. Unclear to me if this was intentional or not, but it was a bit confusing as I was testing things out. My code works fine if I remove that, but seems unexpected and not obvious where the issue is.

Happy to help with exact reproduction if needed but was essentially (net7.0, MacOS):

  1. Install package
  2. Create template with a model
  3. Add ExcludeAssets="compile;runtime" PrivateAssets="all" to the package reference
  4. See constructor remove model parameter

Thanks!

ltrzesniewski commented 1 year ago

Hi! Well, sorry about that, that's a bug - it didn't occur to me to test this particular case. Oops.

I'm happy you like the lib, though I wouldn't expect the embedded library mode to shrink the total application size by much once it's published - it only embeds RazorBlade.dll as source code into your project, after changing its types' accessibility to internal.

ltrzesniewski commented 1 year ago

Note that in the meantime, you can work around this issue by adding the following to your template:

@functions
{
    public YourModelType Model { get; init; }
}

That won't generate a constructor, but at least you'll have a Model property.

greghroberts commented 1 year ago

Thanks for the quick response! For now, I'm just not going to use the option as you pointed out the size difference didn't seem noticeable.

ltrzesniewski commented 1 year ago

This is fixed in v0.4.1 🙂