0

How can I validate Razor template, which is used to render mail template.

string template = @"
My template 
@if (Model.Condition { <span>is true</spaM> }"

I made two mistakes: missing closing bracket and wrong closing tag. I need to validate it and known what should I fix (the best is to known in which line). I would like to use native Razor methods.

ekad
  • 14,436
  • 26
  • 44
  • 46
Jacek
  • 11,661
  • 23
  • 69
  • 123
  • Why do want to store it as a string? Store as a template and [precompile it](http://stackoverflow.com/q/5500078/11683). – GSerg Jul 04 '16 at 18:59

3 Answers3

2

If I understand correctly, you want to be notified that the code you've written in the Template is invalid HTML.

If so, I'm afraid there is no easy way. The Template is purely producing text that you specify to go out to the response.

It may not even be HTML - could be JavaScript or a number of other outputs - it's just a string of text.

You may have a Template that produces the start of a table, another that produces the body, and another that produces the footer and end table tags. Each of these would produce invalid HTML on their own, but output one after the other would produce a valid HTML table. (That's a lot of produces there - sorry).

What would make it invalid is the parser of the HTML - i.e. the browser. You would only be able to validate your Template output when it is in a complete document that can then be parsed.

Steve Padmore
  • 1,710
  • 1
  • 12
  • 18
  • Razor templates must be valid in terms of Razor / C#. That missing closing bracket the OP is showing is a part of the Razor syntax, not of the template. – GSerg Jul 04 '16 at 20:55
  • @gserg, you are correct. That is why I was asking what exactly was required - typing the above in to a DisplayTemplate would show syntax errors anyway. The missing bracket and the incorrect closing span tag would flag up when typing, but would still compile... Still not sure why notifications and line numbers are needed to be notified. – Steve Padmore Jul 04 '16 at 21:13
0

You mean ?

@{
    string template = Model.Condition ? "My template <span>is true</span>" : "";
}
Zebra
  • 66
  • 12
0
string MyString = string.empty;
@if(Model.Condition)
{
MyString ="<span>"+ "is true"+"</span>";
}
devd196
  • 3
  • 6