43 lines
1.0 KiB
C#

using DigitalData.EmailProfilerDispatcher.Domain.Attributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace DigitalData.EmailProfilerDispatcher.Application
{
public static class TemplateExtensions
{
public static string FillTemplate(this string template, params object[] models)
{
foreach (var model in models)
{
var properties = model.GetType().GetProperties();
foreach (var property in properties)
{
var attribute = property.GetCustomAttribute<TemplatePlaceholderAttribute>();
if (attribute != null)
{
var value = property.GetValue(model)?.ToString();
template = template.Replace(attribute.Placeholder, value);
}
}
}
return template;
}
public static string FillTemplate(this string template, Dictionary<string, string> placeholders)
{
foreach (var ph in placeholders)
template = template.Replace(ph.Key, ph.Value);
return template;
}
}
}