I want simple C++ string based template library to replace strings at runtime.
For example, I will use
string template = \"My name is {{name}}\";
Have you considered a set of inline functions that use ostringstram instead of "string templates"?
inline std::string name_template(const std::string& name)
{
std::ostringstream os;
os << "My name is " << name;
return os.str();
}
There are other alternate approaches if you need more generality. For example a class hierarchy where the base provides a "format" interface and child classes implement it with the appropriate varying implementation.
It might be overkill, but you can also take a look at boost::spirit, and more specifically, the 'karma' part which is a text generator.
Can you use sprintf?
There's also boost::format if you want to include boost.
For whom is still looking for such a library I have created a little one https://github.com/lexxmark/string_template.
stpl::string_template st("Hello {{name}}!");
st.set_arg("name", "World");
auto r = st.render();
EXPECT(r, "Hello World!");
It also has some nice customizations.
Update: The project has moved to Github and renamed into CTemplate: https://github.com/OlafvdSpek/ctemplate
From the new project page:
was originally called Google Templates, due to its origin as the template system used for Google search result pages. Now it has a more general name matching its community-owned nature.
Have you tried Google's CTemplate library ? It seems to be exactly what you are looking for: http://code.google.com/p/google-ctemplate/
Your example would be implemented like this:
In example.tpl:
My name is {{name}}
In example.cc:
#include <stdlib.h>
#include <string>
#include <iostream>
#include <google/template.h>
int main(int argc, char** argv)
{
google::TemplateDictionary dict("example");
dict.SetValue("name", "John Smith");
google::Template* tpl = google::Template::GetTemplate("example.tpl",
google::DO_NOT_STRIP);
std::string output;
tpl->Expand(&output, &dict);
std::cout << output;
return 0;
}
Then:
$ gcc example.cc -lctemplate -pthread
$ ./a.out
My name is John Smith
Note that there is also a way to write templates as const strings if you don't want to bother writting your templates in separate files.
If you have many place holders, e.g if you have a macro template for a letter you want to be automatically expanded, some basic tokenization would be easier to maintain, implement and extend later. E.g
//pseudocode
foreach word in line
{
if word=={{name}} print getFromDB(name,entry)
else if word=={{address}} print getFromDB(address,entry)
..
..
else print word
/*
* to get rid of if-else-if tree, you can just check if starts with {{ and ends with }} and directly query the string against a db/map/hash
*/
}
However, if the problem is a simple enough, and the template is small enough, just go for one of the answers mentioned above.