I was wondering if you had some sort of best practices regarding custom templates when extending DynamicWeb functionality through Apps. I'm creating a CheckoutHandler that requires a custom template. The CheckoutHandler is in a separate project but the custom templates are in the Files folder for the Host project. Is there a recommended way to keep the custom template in the CheckoutHandler project that produces the DynamicWeb app that will be installed through the Admin UI appstore.
Developer forum
E-mail notifications
App Addins with custom Templates
Jóhannes Þorkell Tómasson
Replies
Nicolai Pedersen
Posted on 22/01/2024 21:25:20
This post has been marked as an answer
Hi Johannes
A bit untested, but here is how we do it with embedded templates.
Embedding the razor template directly in the dll:
Then the csproj file:
The code to read the contents of that file:
/// <summary>
/// Reads the contents of embedded templates
/// </summary>
public static class DefaultTemplate
{
public static readonly Lazy<string> RecoveryEmail = new(() => GetEmbeddedTemplateHtml($"RecoveryEmail.cshtml"));
private static string GetEmbeddedTemplateHtml(string templateName)
{
string templateHtml;
var assembly = typeof(DefaultTemplate).Assembly;
using (var stream = assembly.GetManifestResourceStream(typeof(DefaultTemplate), templateName))
{
using (var reader = new StreamReader(stream))
{
templateHtml = reader.ReadToEnd();
}
}
return templateHtml;
}
}
And the use of it:
Or in code:
//var template = new Template($"UserManagement/Login/{mailTemplate}");
Template template = new() { Html = UserManagement.DefaultTemplates.DefaultTemplate.RecoveryEmail.Value,
Type = Template.TemplateType.Razor };
var userViewModel = ContentViewModelFactory.CreateUserViewModel(user);
var userRecoveryViewModel = new UserRecoverPasswordViewModel()
{
RecoveryToken = token,
RecoveryTokenExpiration = expirationInMinutes,
RecoveryUrl = recoveryUrl,
User = userViewModel
};
template.SetViewModel(userRecoveryViewModel);
message.Body = template.Output();
Votes for this answer: 1
Jóhannes Þorkell Tómasson
Posted on 24/01/2024 13:09:41
Great, thanks.
You must be logged in to post in the forum