0

I have already looked at the post: Efficient plain text template engine, but it didn't answer my question. It's documentation is more than a little lacking, and I don't see that it does what I'm trying to do.

I'm wondering if you can iterate over a template and fill in the values with a function, whose parameters come from attributes within the template. e.g.:

"The <comparison property='fruit' value='green'> and the <comparison property='bowl' value='big'>."

becomes, after iterating over each variable and passing it to a function,

"The fruit is green and the bowl is big."

I'm trying to generate a css page based upon a JSON object containing appearance settings.

EDIT: I'm wondering if there's a way to get the straight object from JsonConvert.DeserializeObject(). The JObject has a lot of information I don't need.

Community
  • 1
  • 1
AaronF
  • 2,841
  • 3
  • 22
  • 32
  • 1
    Have you googled to find more docs? https://razorengine.codeplex.com/documentation. Also see http://www.w3schools.com/aspnet/razor_intro.asp – L.B Aug 21 '14 at 18:12
  • Yes, but then I saw the link saying that the code is being moved to GitHub and an article indicating that breaking changes are being made in version 3. That said, I'm probably not doing anything complex enough for that to matter, so thanks! I'm adding an edit, focusing my question according to your input. – AaronF Aug 21 '14 at 18:26

1 Answers1

2

(I am not sure if this is what you are looking for, but) I guess, you can combine my previous answer (showing the use of JObject.SelectToken) with regex to create your own templating engine.

string Parse(string json, string template)
{
    var jObj = JObject.Parse(json);
    return Regex.Replace(template, @"\{\{(.+?)\}\}", 
                         m => jObj.SelectToken(m.Groups[1].Value).ToString());
}

string json = @"{name:""John"" , addr:{state:""CA""}}";
string template = "dummy text. Hello {{name}} at {{addr.state}} dummy text.";

string result = Parse(json, template);
Community
  • 1
  • 1
L.B
  • 114,136
  • 19
  • 178
  • 224