1

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?

ProgrammingLlama
  • 36,677
  • 7
  • 67
  • 86
João Gomes
  • 312
  • 2
  • 12

1 Answers1

1

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 ;)

iJungleBoy
  • 5,325
  • 1
  • 9
  • 21