toddams / RazorLight

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

Using a layout file with embedded resources #385

Closed Eonasdan closed 3 years ago

Eonasdan commented 3 years ago

Hi. Thanks for this great project. It makes send nicely formatted emails so much easier.

Using the file system engine, I can include something like this in one of my templates

@{
    Layout = "_Layout";
}

I don't want to relay the file system in prod so I'm looking at changing the engine to use embedded resources, however this results in an exception of Project can not find template with key _Layout.cshtml. Both files are embedded. Am I missing something here? I didn't see much in the way of docs for layout files.

maxbanas commented 3 years ago

What version/platform are you using? This seems to work for me with the latest from master and dotnet core 3.1.

Eonasdan commented 3 years ago

I'm using the latest from nuget (2.0.0-beta9). Let me see if I can get a small repo to reproduce

Eonasdan commented 3 years ago

https://github.com/Eonasdan/razorlight-385

There are two method in the service. One of them uses a path (which you'd have to adjust) and the other tries to use the templates via the embeds. The embed fails with the exception above.

maxbanas commented 3 years ago

For your embedded resource project, the key for _Layout.cshtml would be "EmailTemplates._Layout.cshtml". So in NotificationDigest.cshtml, you would set the layout like this:

@{
    Layout = "EmailTemplates._Layout.cshtml";
}

If all your templates are within EmailTemplates/, and you don't want to have to include "EmailTemplates" in the key, you can set the root namespace by doing something like this: .UseEmbeddedResourcesProject(typeof(Test).Assembly, "RazorLitePoc.Services.EmailTemplates")

Also see #378

Eonasdan commented 3 years ago

Thanks that was the ticket

photomoose commented 3 years ago

In case other people come here wondering why this isn't working, note that there's a DOT between the folder name ("EmailTemplates") and the layout name ("_Layout.cshtml"), and not a SLASH.