CarterCommunity / Carter

Carter is framework that is a thin layer of extension methods and functionality over ASP.NET Core allowing code to be more explicit and most importantly more enjoyable.
MIT License
2.11k stars 174 forks source link

Provide a view engine or at least a sample if it already exists #242

Closed mika76 closed 2 years ago

mika76 commented 4 years ago

I see there is a carter mvc sample but it's missing the V part - does Carter provide some sort of View engine (I see nothing mentioned in the repo or in the issues), if not is it planned? if so could you provide a sample?

jchannon commented 4 years ago

It doesn’t. Razor is too tied with MVC to be separated out. We did have a plug-in to work with handlebars which stalled but would be happy for someone to give it a go

On Sat, 7 Mar 2020 at 08:40, Mladen Mihajlović notifications@github.com wrote:

I see there is a carter mvc sample but it's missing the V part - does Carter provide some sort of View engine (I see nothing mentioned in the repo or in the issues), if not is it planned? if so could you provide a sample?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/CarterCommunity/Carter/issues/242?email_source=notifications&email_token=AAAZVJX7FOXNVJMOSB7RB33RGIB6JA5CNFSM4LDNWOH2YY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4ITI5VAQ, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAAZVJROID2KNRLHF7GCCULRGIB6JANCNFSM4LDNWOHQ .

mika76 commented 4 years ago

Interesting I see Nancy uses this one https://github.com/grumpydev/SuperSimpleViewEngine

DirkJanIT commented 4 years ago

I see there is a carter mvc sample but it's missing the V part - does Carter provide some sort of View engine (I see nothing mentioned in the repo or in the issues), if not is it planned? if so could you provide a sample?

Two options that work reliably.

  1. You can mix MVC controllers (and their views) with Carter, use the MVC sample as base and add AddRazorPages and MapRazorPages.
  2. Use RazorLight (or any other Razor compiler). It's a bit more work but easy enough to get going and better integrates with Carter routes.
jchannon commented 4 years ago

The plugin I mention above is here https://github.com/CarterCommunity/Carter.HtmlNegotiator/blob/master/src/HtmlNegotiator.cs

DirkJanIT commented 4 years ago

I've been experimenting a bit more with MVC controllers and found out you can use the controllers just for the views, as long as you don't use the same route twice, such as /view in a controller and /view in a module. What works fine is /view for controller and Get/Post("/view/xyz") in the module. I'm now using a controller just to serve the views. All json stuff is done by the modules. The upside is that this is much faster than using RazorLight (response times are less than 20% of the time with RazorLight).

jchannon commented 4 years ago

If you'd like to send a PR to amend the CarterAndMVC sample we have, I'd love to see that - https://github.com/CarterCommunity/Carter/tree/master/samples/CarterAndMVC

mindcrash commented 3 years ago

With a few small adjustments (due to some breaking naming changes in .NET Core 3) you can make the HTML Negotiator work (mentioned earlier) with the latest release.

mika76 commented 3 years ago

I also found https://github.com/lunet-io/scriban which is a text templating engine which might be able to work...

dlwiii commented 3 years ago

@DirkJanIT , do you have any example code using Razor? We have a large Nancy application that we are looking to migrate over, but its view are all Razor-based.

PhyxionNL commented 3 years ago

I'm also using Carter with built-in Razor. You'll basically need to add the standard MVC code to Startup (https://github.com/CarterCommunity/Carter/blob/master/samples/CarterAndMVC/Startup.cs), for example: https://gist.github.com/PhyxionNL/61cedf66cf704f55eff985ceac0c0199

This way you can use controllers for views and modules for the rest (json/xml/whatever you'd like to serve). You obviously cannot use the same route twice, so you need to keep track of which routes are used. It's probably possible to return a view from a carter module with a bit of code, but I just use a controller for serving the views.