I am in need of a very lightweight, fast, C++ template engine. I have been testing CTemplate and it fits my needs, yet it is a little slow. I have checked out many of the other template engines that have been recommended on this site, but most of them are more complex that CTemplate, and I am looking for the opposite. All I really need is simple text substitution, yet would prefer to use an existing engine. I also need a relaxed license, preferably MIT or BSD.
EDIT: Have looked into the following: ClearSilver, Teng, Templatizer, CTPP (This is a little complex to me... I pretty new to C++ and the linux dev environment) qctemplate, and more, just have to try and remember them
Created one, since I too dislike having boost as a dependency :-)
https://github.com/hughperkins/Jinja2CppLight
You can try syslandscape-tmpl.
Project provide flexible and powerfull template engine for C++.
A tree-like data structre is used for storing template variables. Variables can be int, string, list or object.
Features
Requirements
C++11
Template Storage
Currently the engine supports only string
and file
storage for templates.
Example
#include <iostream>
#include <memory>
#include <syslandscape/tmpl/data.h>
#include <syslandscape/tmpl/engine.h>
#include <syslandscape/tmpl/string_storage.h>
using syslandscape::tmpl::data;
using syslandscape::tmpl::engine;
using syslandscape::tmpl::storage;
using syslandscape::tmpl::string_storage;
int main()
{
std::shared_ptr<string_storage> storage = std::make_shared<string_storage>();
storage->put_template("/greetings.html", "Hello, I'm {$person.name}.");
storage->put_template("/city.html", "I'm from {$person.city}.");
storage->put_template("/age.html", "I'm {$person.age} years old.");
storage->put_template("/examples.html", "\n{$message}: "
"{% for item in data %}{$item}{%endfor%}");
storage->put_template("/index.html",
"{%include /greetings.html%} "
"{%include /city.html%} "
"{%include /age.html%} "
"{%include /examples.html%}");
engine e;
e.set_storage(storage);
data model;
model["person"]["name"] = "Alex";
model["person"]["city"] = "Montana";
model["person"]["age"] = 3;
model["message"] = "List example";
model["data"].append("Answer is");
model["data"].append(" 42");
std::cout << e.process("/index.html", model) << std::endl;
return 0;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With