Import from internal git
This commit is contained in:
7
Core/Templates/AbstractTemplateFactory.cs
Normal file
7
Core/Templates/AbstractTemplateFactory.cs
Normal file
@@ -0,0 +1,7 @@
|
||||
namespace Core.Templates;
|
||||
|
||||
public interface ITemplateFactory
|
||||
{
|
||||
public ITemplate GetTemplate(string templatePath, IDictionary<string, object> data);
|
||||
|
||||
}
|
||||
9
Core/Templates/ITemplate.cs
Normal file
9
Core/Templates/ITemplate.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
using System.Collections;
|
||||
|
||||
namespace Core.Templates;
|
||||
|
||||
public interface ITemplate
|
||||
{
|
||||
public IEnumerable GetData();
|
||||
public string GetText();
|
||||
}
|
||||
23
Core/Templates/MustacheTemplate.cs
Normal file
23
Core/Templates/MustacheTemplate.cs
Normal file
@@ -0,0 +1,23 @@
|
||||
using System.Collections;
|
||||
namespace Core.Templates;
|
||||
|
||||
public class MustacheTemplate : ITemplate
|
||||
{
|
||||
private readonly string _text;
|
||||
private readonly TemplateData<string, object> _data;
|
||||
|
||||
public MustacheTemplate(string text)
|
||||
{
|
||||
_text = text;
|
||||
_data = new TemplateData<string, object>();
|
||||
}
|
||||
|
||||
public void AddProperties(IDictionary<string, object> data)
|
||||
{
|
||||
_data.AddAll(data);
|
||||
}
|
||||
|
||||
public IEnumerable GetData() => _data.GetData();
|
||||
|
||||
public string GetText() => _text;
|
||||
}
|
||||
19
Core/Templates/MustacheTemplateFactory.cs
Normal file
19
Core/Templates/MustacheTemplateFactory.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using Core.Helpers;
|
||||
|
||||
namespace Core.Templates;
|
||||
|
||||
public class MustacheTemplateFactory : ITemplateFactory
|
||||
{
|
||||
public MustacheTemplateFactory()
|
||||
{
|
||||
}
|
||||
|
||||
public ITemplate GetTemplate(string templatePath, IDictionary<string, object> data)
|
||||
{
|
||||
var text = PathHelper.TextFrom(templatePath);
|
||||
var template = new MustacheTemplate(text);
|
||||
template.AddProperties(data);
|
||||
|
||||
return template;
|
||||
}
|
||||
}
|
||||
29
Core/Templates/TemplateData.cs
Normal file
29
Core/Templates/TemplateData.cs
Normal file
@@ -0,0 +1,29 @@
|
||||
using Services.Common.ObjUtils;
|
||||
|
||||
namespace Core.Templates;
|
||||
|
||||
public class TemplateData<TK, TV> where TK : notnull
|
||||
{
|
||||
private IDictionary<TK, TV> _data;
|
||||
|
||||
public TemplateData()
|
||||
{
|
||||
_data = new Dictionary<TK, TV>();
|
||||
}
|
||||
|
||||
public void Add(TK key, TV value)
|
||||
{
|
||||
_data.Add(key, value);
|
||||
}
|
||||
|
||||
public void AddAll(IDictionary<TK, TV> properties)
|
||||
{
|
||||
foreach (var item in properties)
|
||||
{
|
||||
_data.Add(item);
|
||||
}
|
||||
}
|
||||
|
||||
public IDictionary<TK, TV> GetData() => _data.Clone();
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user