Template.cs 719 B

1234567891011121314151617181920212223242526272829303132
  1. using System;
  2. using System.Text.RegularExpressions;
  3. namespace Masuit.Tools.Strings
  4. {
  5. public class Template
  6. {
  7. private string Content { get; set; }
  8. public Template(string content)
  9. {
  10. Content = content;
  11. }
  12. public Template Set(string key, string value)
  13. {
  14. Content = Content.Replace("{{" + key + "}}", value);
  15. return this;
  16. }
  17. public string Render()
  18. {
  19. var mc = Regex.Matches(Content, @"\{\{.+?\}\}");
  20. foreach (Match m in mc)
  21. {
  22. throw new ArgumentException($"模版变量{m.Value}未被使用");
  23. }
  24. return Content;
  25. }
  26. }
  27. }