toddams / RazorLight

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

AddDynamicTemplates for embedded resources #534

Open sybaris opened 8 months ago

sybaris commented 8 months ago

Is your feature request related to a problem? Please describe. I want to use Layout but with embedded resources. Currenlty, I use RazorLight without layouts. It works.

My current code is

        internal static string GenerateHtml<T>(string resourceName, T model)
        {
            var engine = new RazorLightEngineBuilder()
                .UseEmbeddedResourcesProject(typeof(RootTypeForEmbeddedResources).Assembly, typeof(RootTypeForEmbeddedResources).Namespace)
                //.AddDynamicTemplates(new Dictionary<string,string>()
                //{ {"_Layout.cshtml","<body>Test\r\n    @RenderBody()\r\n</body>" }}
                //)
                .SetOperatingAssembly(operatingAssembly)
                .UseMemoryCachingProvider()
                .EnableDebugMode()
                .Build();

            string html = engine.CompileRenderAsync<T>(resourceName, model).Result;
            return html;
        }

I test, if I use AddDynamicTemplates (commented code), it works too. No I want to have a file "_Layout.cshtml" which is an embedded ressource. Currently if I do nothing, I have the following Exception : "One or more errors occurred. (RazorLightProjectItem of type RazorLight.Razor.EmbeddedRazorProjectItem with key _Layout.cshtml could not be found by the RazorLightProject of type RazorLight.Razor.EmbeddedRazorProject and does not exist in dynamic templates. See the "KnownDynamicTemplateKeys" and "KnownProjectTemplateKeys" properties for known template keys.) ---> RazorLight.TemplateNotFoundException: RazorLightProjectItem of type RazorLight.Razor.EmbeddedRazorProjectItem with key _Layout.cshtml could not be found by the RazorLightProject of type RazorLight.Razor.EmbeddedRazorProject and does not exist in dynamic templates. See the "KnownDynamicTemplateKeys" and "KnownProjectTemplateKeys" properties for known template keys."

And KnownDynamicTemplateKeys is empty, and KnownProjectTemplateKeys does not contains my "_Layout.cshtml"

Describe the solution you'd like Several solutions : 1°/ Create a "UseEmbeddedResourcesTemplate" like "UseEmbeddedResourcesProject" 2°/ Create a AddDynamicTemplatesFromResources ...

Additional context

sybaris commented 7 months ago

Hi,

So currently, I do this :


        public static string ReadEmbeddedResource(Assembly assembly, string resourceName)
        {
            using (Stream resourceStream = assembly.GetManifestResourceStream(resourceName))
            using (StreamReader reader = new StreamReader(resourceStream))
            {
                return reader.ReadToEnd();
            }
        }

        internal static string GenerateHtml<T>(string resourceName, T model)
        {
            var rootType = typeof(RootTypeForEmbeddedResources);
            var rootTypeAssembly = rootType.Assembly;
            var rootTypeNamespace = rootType.Namespace;

            var layoutResource = ReadEmbeddedResource(rootTypeAssembly, rootTypeNamespace + "._Layout.cshtml");
            var engine = new RazorLightEngineBuilder()
                .UseEmbeddedResourcesProject(typeof(RootTypeForEmbeddedResources).Assembly, typeof(RootTypeForEmbeddedResources).Namespace)
                .AddDynamicTemplates(new Dictionary<string,string>()
                    {
                        {"_Layout.cshtml",layoutResource },
                    })
                .SetOperatingAssembly(operatingAssembly)
                .UseMemoryCachingProvider()
                .EnableDebugMode()
                .Build();

            string html = engine.CompileRenderAsync<T>(resourceName, model).Result;
            return html;
        }