Razorlight: Using a layout file with embedded resources

Created on 6 Nov 2020  路  6Comments  路  Source: toddams/RazorLight

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.

Most helpful comment

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

All 6 comments

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

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

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.

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

Thanks that was the ticket

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.

Was this page helpful?
0 / 5 - 0 ratings