adoconnection / RazorEngineCore

.NET6 Razor Template Engine
MIT License
576 stars 85 forks source link

Support for implementing @section, @RenderSection #73

Open kftrans opened 3 years ago

kftrans commented 3 years ago

These changes allow implementation of templates supporting @section and @RenderSection, which are quite widely used in razor documents, so that support may be needed if porting existing apps to RazorEngineCore. Includes an example based on the layout example at https://github.com/adoconnection/RazorEngineCore/wiki/@Include-and-@Layout (run LayoutAndSections.Test )

ItWorksOnMyMachine commented 9 months ago

@adoconnection, will you accept this PR? I currently have a fork of this repo plus this update because of our broad @section use. It would be very useful to have this accepted into the main project. Thanks!

adoconnection commented 9 months ago

Hi @ItWorksOnMyMachine could you please share how exactly are you using it? Must be something similar to web server where you render generic template with random Layout + sections, right? Are you on NET6+ ?

For ASP.NET MVC there is strong reason Layout and Section are located in cshtml files: since controller is not resonsible for templating, cshtml file is the only place you can choose layout.

For RazorEngineCore on the other hand there is no files, so RazorEngineCore will have to go somewhere to grab Layout, it seems like a overcomlication.

Making ProjectEngineBuilderAction accessable seems to be good improvement. I was in doubt thats why its not yet merged.

ItWorksOnMyMachine commented 9 months ago

Hi @adoconnection, We are transitioning from net48 to net6 and the final bits that use net48 are from Antaris RazorEngine. We use this to generate emails for our customers. We have about 100(ish) different email templates and 10 or so different layouts we use depending on customer type. Most of these layouts use RenderSection() to include email specific html if/when relevant. Around 30 of our emails include an @section directive for things like custom login buttons and other header bits that are unique to specific user types. I forked this repo and applied this PR and everything works fine. I suppose just adding access to ProjectEngineBuilderAction would allow all of this to be done in a custom template implementation. So, perhaps that's a better solution considering how little template support you want to add directly?

ItWorksOnMyMachine commented 6 months ago

@adoconnection, any thoughts on making ProjectEngineBuilderAction public? Support for sections can be left out if ProjectEngineBuilderAction is public because we can then provide our own support in our Templates. I'm currently maintaining a fork of this project just so I can access ProjectEngineBuilderAction in order to do just this.

adoconnection commented 6 months ago

Hi @ItWorksOnMyMachine, ProjectEngineBuilderAction should go public for sure 👍👍

ItWorksOnMyMachine commented 5 months ago

Hi @ItWorksOnMyMachine, ProjectEngineBuilderAction should go public for sure 👍👍

I've create a new PR with just this and no extra functionality. https://github.com/adoconnection/RazorEngineCore/pull/141