Search code examples
c#razor2sxc

How to use App.Resources in a mail template


I'm using the 2sxc forms app to send emails, but I need to use App.Resoures in the mail template.

I use this:

var templateMailEngine = TemplateInstance("mailtemplate.cshtml");
var templateBody = templateMailEngine.Message(contactFormRequest, this).ToString();

private dynamic TemplateInstance(string fileName)
{
    var compiledType = BuildManager.GetCompiledType(System.IO.Path.Combine("~", App.Path, fileName));
    object objectValue = null;
    if (compiledType != null)
    {
        objectValue = RuntimeHelpers.GetObjectValue(Activator.CreateInstance(compiledType));
        return ((dynamic)objectValue);
    }
    throw new Exception("Error while creating mail template instance.");
}

And the template:

@helper Message(Dictionary<string,string> request, ToSic.SexyContent.IAppAndDataHelpers context)
{
    <!doctype html>
    <html>
    <head>
        <meta name="viewport" content="width=device-width">
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <style type="text/css">
            body { font-family: Helvetica, sans-serif; }
        </style>
    </head>
    <body>
        <h1>@App.Resources.InformedConsent</h1>
    </body>
</html>
}

As soon as I type "@App.Resources.InformedConsent" I get a "Object reference not set to an instance of an object."

Is there a way to implement this?


Solution

  • So you are using an older version of Mobius - the latest version (only on github, release in the next few weeks) uses CreateInstance to activate the razor. There it will be easier.

    BUT in your case, I think the context variable has everything. I think you can just do context.App.Resources.InformedConsent ;)