Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In need of a lightweight C++ Template Engine [closed]

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

like image 614
David Avatar asked Jun 15 '12 17:06

David


2 Answers

Created one, since I too dislike having boost as a dependency :-)

https://github.com/hughperkins/Jinja2CppLight

  • uses Jinja2 syntax
  • supports variable substitution, and for loop
  • can be nested for-loops :-)
  • zero dependencies, just c++ and standard libraries :-)
like image 69
Hugh Perkins Avatar answered Nov 03 '22 01:11

Hugh Perkins


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

  • variables
  • for loops and nested for loops
  • if and nested if
  • include other templates

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;
}
like image 1
Valchev Avatar answered Nov 03 '22 01:11

Valchev