【问题标题】:In need of a lightweight C++ Template Engine [closed]需要一个轻量级 C++ 模板引擎 [关闭]
【发布时间】:2012-06-15 17:43:48
【问题描述】:

我需要一个非常轻量级、快速的 C++ 模板引擎。我一直在测试 CTemplate,它符合我的需求,但速度有点慢。我已经查看了该站点上推荐的许多其他模板引擎,但它们中的大多数都比 CTemplate 更复杂,我正在寻找相反的东西。我真正需要的是简单的文本替换,但更喜欢使用现有的引擎。我还需要一个宽松的许可证,最好是 MIT 或 BSD。

编辑:已研究以下内容: 清银, 腾, 模板, CTPP(这对我来说有点复杂......我对 C++ 和 linux 开发环境很陌生) qctemplate等等,只需要试着记住它们

【问题讨论】:

  • 您还尝试了哪些其他模板?你不希望人们重复你已经尝试过的事情。
  • 有哪些用例?嵌入式?软件开发?您希望引擎做什么的示例?
  • 这是一个:github.com/catnapgames/NLTemplate - 非常基本的单一源文件。
  • 您所说的templates 一词与大多数人在C++ 上下文中想到的templates 一词有很大不同。也许您可以更清楚地说明您在问题中寻找的模板类型。
  • @TomA:NLTemplate 看起来不错 :-)

标签: c++ template-engine


【解决方案1】:

创建了一个,因为我也不喜欢将 boost 作为依赖项 :-)

https://github.com/hughperkins/Jinja2CppLight

  • 使用 Jinja2 语法
  • 支持变量替换和for循环
  • 可以嵌套for循环:-)
  • 零依赖,只有 c++ 和标准库 :-)

【讨论】:

  • 我是否理解正确,这取决于本地 Python 安装?
  • 不需要 python,不使用 Python。它为 C++ 实现了一个非常轻量级的 Jinja2 版本,用 C++ 编写。
【解决方案2】:

你可以试试syslandscape-tmpl

项目为C++提供灵活强大的模板引擎。

树状数据结构用于存储模板变量。变量可以是 int、string、list 或 object。

特点

  • 变量
  • for 循环和嵌套 for 循环
  • if 和嵌套 if
  • 包括其他模板

要求

C++11

模板存储

目前该引擎仅支持模板的stringfile 存储。

示例

#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;
}

【讨论】:

  • 链接已失效,项目似乎不存在。
猜你喜欢
  • 2011-02-06
  • 1970-01-01
  • 1970-01-01
  • 2010-12-03
  • 2011-03-26
  • 1970-01-01
  • 1970-01-01
  • 2013-01-31
  • 1970-01-01
相关资源
最近更新 更多