3

I have a simple alert system that grabs number on the web, mix them with pre-defined text template to get an alert, and send it to clients. The alert is quite simple plain text, so I would not expect much other than plain text, numbers, simple functions(such as ifthenelse), the quicker the better. So are there any existing open source solutions for this? Thanks!

captivatedbyUBB
  • 153
  • 2
  • 9
  • have you had a look at T4 ? http://www.hanselman.com/blog/T4TextTemplateTransformationToolkitCodeGenerationBestKeptVisualStudioSecret.aspx – DarkSquirrel42 Apr 25 '13 at 09:21

1 Answers1

9

I would use Razor Engine for this.

A templating engine built upon Microsoft's Razor parsing technology. The RazorEngine allows you to use Razor syntax to build robust templates

A simple example from its page:

string template = "Hello @Model.Name! Welcome to Razor!";
string result = Razor.Parse(template, new { Name = "World" });

and result will be Hello World! Welcome to Razor!

I4V
  • 34,891
  • 6
  • 67
  • 79
  • 2
    sorry, I just tried it, but it is too slow for my usage, It took 200 ms to complete this execution, I need an engine that is not as power, but quick enough(less than 10 ms for such an example) – captivatedbyUBB Apr 25 '13 at 09:41
  • 1
    @captivatedbyUBB you should measure the performance after the first iteration. First one includes the JIT time... – I4V Apr 25 '13 at 09:49
  • @captivatedbyUBB: You should be able to reach well under 10ms if don't measure the one-time cost of template creation and compilation, like I4V said. Also, I've found anonymous types to cause performance issues with Razor. Dynamic types have the same problem (those that inherit from `DynamicObject` or implement `IDynamicMetaObjectProvider`). – Allon Guralnek Apr 25 '13 at 09:56
  • @captivatedbyUBB you can also pre-compile templates. See `Razor.Compile` and `Razor.Run` – I4V Apr 25 '13 at 10:19
  • The whole simple interface of RazorEngine (eg. Razor.Parse) is now marked as deprecated. I failed to figure out how to use new APIs, so I'll search further, I guess... – Spook Feb 19 '15 at 11:53
  • Why did they switch from this simple implementation to their much more complex one? – IronSean Apr 06 '17 at 19:18