I want simple C++ string based template library to replace strings at runtime.
For example, I will use
string template = \"My name is {{name}}\";
>
If you have a function that replaces all occurrences of a string with another string:
std::string replace_all(std::string str, const std::string &remove, const std::string &insert)
{
std::string::size_type pos = 0;
while ((pos = str.find(remove, pos)) != std::string::npos)
{
str.replace(pos, remove.size(), insert);
pos++;
}
return str;
}
Then you can do this:
std::string pattern = "My name is {{first_name}} {{last_name}} and I live in {{location}}";
std::string str = replace_all(replace_all(replace_all(pattern,
"{{first_name}}", "Homer"),
"{{last_name}}", "Simpson"),
"{{location}}", "Springfield");