您可以尝试syslandscape-tmpl。
项目为 C++ 提供灵活而强大的模板引擎。
树状数据结构用于存储模板变量。变量可以是 int、string、list 或 object。
特征
- 变量
- for 循环和嵌套 for 循环
- if 和嵌套 if
- 包括其他模板
要求
C++11
模板存储
目前引擎只支持模板string
和file
存储。
例子
#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;
}