aspnetcore-angular-universal icon indicating copy to clipboard operation
aspnetcore-angular-universal copied to clipboard

Ad-Hoc Template Rendering

Open OpenSpacesAndPlaces opened this issue 8 years ago • 2 comments

How would I use this to just render templates with?

We're building an MVC site. We'd like to streamline to use one template format. (rather than needing to know Razor, Angular, others)

Trying to look a feasibility of doing this with angular, but we don't need a entire angular app to run - we just want to be able to ad-hoc specify a template path and a data object - that's it.

In Razor I can do something like:

var viewResult = _razorViewEngine.GetView("~", viewPath, false);
....
var viewContext = new ViewContext(
					actionContext,
					viewResult.View,
					viewDictionary,
					new TempDataDictionary(actionContext.HttpContext, _tempDataProvider),
					sw,
					new HtmlHelperOptions()
				);
await viewResult.View.RenderAsync(viewContext);
...

Any help would be appreciated.

OpenSpacesAndPlaces avatar Jan 09 '18 22:01 OpenSpacesAndPlaces

Hey there 👋 You could remove all client-side rendering from happening.

Don't have any javascript bundles brought down in the _layout & index.cshtml View files, and have your Angular components simply render on the server, rendered into the <app-root> tag, and then nothing else will happen.

For example, DevHelp Online is done doing exactly that. It's just a static server rendered Angular (1-page) app, no JS is loaded, so no client side bootstrapping happens.

Just note that any JS functionality you expect to exist won't be there, so you need to be selective with what you're components & templates generate :)

Good luck! 👍

MarkPieszak avatar Jan 11 '18 22:01 MarkPieszak

@MarkPieszak

Thanks for the reply!

That sounds exactly like what I'm looking for - do you happen to have a sample project or a few C# snippets for what that looks like?

The closest thing I've come across is like:

ISpaPrerenderer spaRender;
....
RenderToStringResult rtsrResult = await _spaRender.RenderToString("./templates/angularhomepage/default", customDataParameter: eo);

But that still relies on running through js/ts file - and it sounds like you have a way to eliminate that step?

Talk soon and thanks again for the help!

OpenSpacesAndPlaces avatar Jan 12 '18 01:01 OpenSpacesAndPlaces