I have a text template with placehoders that I parse in order to replace placeholders
with real values.
Text Template:
Name:%name%
Age:%age%
I use StringBuilder.Replace() to replace placeholders
sb.Replace("%name%", Person.Name);
Now I want to make more advanced algorithm. Some lines of code are conditional. They
have to be either removed completely of kept.
Text Template
Name:%Name%
Age:%age%
Employer:%employer%
The line Employer should appear only when person is employed (controlled by boolean variable Person.IsEmployed).
UPDATE: I could use open/close tags. How can find text between string A and B?
Can I use Regex? How?
Your current templating scheme isn’t robust enough – you should add more special placeholders, like this for example:
You can parse out [if *] blocks using a regex (not tested):
Really, though, this implementation is full of holes… You may be better off with a templating framework like another answer suggested.