toddams / RazorLight

Template engine based on Microsoft's Razor parsing engine for .NET Core
Apache License 2.0
1.51k stars 260 forks source link

Add example for EmbeddedResource source #319

Closed joshnoe closed 4 years ago

joshnoe commented 4 years ago

RazorLight support Embedded Resources, but the example is missing the call to await engine.CompileRenderAsync so it's unclear how to use this source.

jzabroski commented 4 years ago

@joshnoe Hi, can you use the GitHub deep linking of code feature to link me to which example you're referring to? On the left hand side bar of the code you can say "reference in new issue" and it will create an issue with the deep link. You can then copy-paste that here, thank you for your help and will try to get back quickly.

zevero commented 4 years ago

I tried await engine.CompileRenderAsync(resourceName, model) with

var xmlName = "MyTemplate.xml";
var assembly = Assembly.GetExecutingAssembly();
string resourceName = assembly.GetManifestResourceNames().Single(str => str.EndsWith(xmlName));

but I only receive false

Did you find the solution?

jzabroski commented 4 years ago

@zevero - I'm confused. What do you mean, you only receive false? CompileRenderAsync returns a string, not a bool type. Nothing in your example returns a bool, either?

zevero commented 4 years ago

@jzabroski oh sorry I think I received null or i catched null - dont remeber exactly, but @joshnoe is right, that it is missing in docs. I worked around parsing the empedded ressource to string myself.

Programm3r commented 4 years ago

In version 3.0.2 one of the implementations of CompileRenderAsync has been marked as obsolete.

My current implementation is as follows:

            var razorLightEngine = new RazorLightEngineBuilder()
                .UseEmbeddedResourcesProject(typeof(ReportBase))
                .UseMemoryCachingProvider()
                .Build();

            var template = typeof(ReportBase).Assembly.GetEmbeddedResourceByKey(this.ReportTemplateKey);
            var result = await razorLightEngine.CompileRenderAsync(Guid.NewGuid().ToString(), template, model);

But this implementation no longer works. If I implement this method, which is stated as the one to use:

Task<string> CompileRenderAsync<T>(string key, T model, ExpandoObject viewBag = null);

How would I specify my template (aka my html / view)

Thanks in advance!

viktors-telle commented 4 years ago

Please add an example of how to properly format key in await engine.CompileRenderAsync(...) method when using EmbeddedResource as the source.

https://github.com/toddams/RazorLight#embeddedresource-source

jzabroski commented 4 years ago

@Programm3r Thanks for your example. I am a bit confused when you say "In version 3.0.2", since RazorLight is still on RazorLight-2.0.0-beta releases.

jzabroski commented 4 years ago

I pushed an example - but I have to say I was pretty confused by this request/discussion, so it's possible the example will need to be updated further as I understand what people want help with.