64

I am looking for template/generator libraries for C++ that are similar to eg. Ruby's Erb, Haml, PHP's Smarty, etc.

It would be great if I it would sport some basic features like loops, if/else, int conversion to strings, etc.

Parameter passing to template rendering engine is also important if I could pass all of them in a hash map instead of calling some function for each of parameters.

Do you have any recommendations?

I can see also the possibility of embedding languages like Lua, however I haven't found a templatizing library for that either.

Ajay
  • 18,086
  • 12
  • 59
  • 105
Marcin Gil
  • 68,043
  • 8
  • 59
  • 60

13 Answers13

53

A quick review of the mentioned project.

http://rgrz.tumblr.com/post/13808947359/review-of-html-template-engines-in-c-language

ClearSilver

Teng

Templatizer

  • Site: http://www.lazarusid.com/libtemplate.shtml
  • Project: download only
  • Group: none
  • License: free to use
  • Language: C (low level)/C++ (interface) mixed
  • Last Update: unknown
  • Last Release: unknown
  • Document: none
  • Community: none

HTML Template C++

ctpp

  • Site: http://ctpp.havoc.ru/en/
  • Project: download only
  • Group: none
  • License: BSD License
  • Language: C++ with C API
  • Last Update: Oct 5, 2011
  • Last Release: Version 2.7.2 on Oct 5, 2011
  • Document: Rich
  • Community: none

Wt

Flate

  • Site: http://flate.dead-inside.org/
  • Project: none
  • Group: none
  • License: LGPL v2.1
  • Language: C
  • Last Update: Sep 4, 2010
  • Last Release: 2.0 on Sep 4, 2010
  • Document: Poor
  • Community: none

Jinja2C++

Marcin Gil
  • 68,043
  • 8
  • 59
  • 60
8

Grantlee is a string template engine based on the Django template system. It is ported to C++/Qt.

Kristian
  • 6,357
  • 4
  • 36
  • 37
7

NLTemplate is a small C++ template library with syntax similar to Django.

  • Variable replacement
  • Repeatable or optional blocks
  • File includes
  • MIT licensed
  • No external dependencies
  • Single source file, easy to add to any project

Disclaimer: I'm the author.

Tomas Andrle
  • 13,132
  • 15
  • 75
  • 92
3

Wt (pronounced 'witty') is a C++ library and application server for developing and deploying web applications. It is not a 'framework', which enforces a way of programming, but a library.

yesraaj
  • 46,370
  • 69
  • 194
  • 251
3

CTPP is very fast and powerful library written in C++. It has bindings for Perl, PHP and Python.

Karim
  • 18,347
  • 13
  • 61
  • 70
2

facebook's format:

std::cout << format("The answers are {} and {}", 23, 42); 
// => "The answers are 23 and 42"

std::map<std::string, std::string> m { {"what", "answer"}, {"value", "42"} }; 
std::cout << vformat("The only {what} is {value}", m); 
// => "The only answer is 42"
kirill_igum
  • 3,953
  • 5
  • 47
  • 73
2

ClearSilver is available for c. Here is a list of existing websites which use clearsilver. But I don't use it myself.

wimh
  • 15,072
  • 6
  • 47
  • 98
1

ctemplate

https://code.google.com/p/ctemplate/?redir=1

New BSD License

OJW
  • 4,514
  • 6
  • 40
  • 48
1

Somehow I missed NLTemplate when I was searching originally, and wrote my own C++ templating engine, with apparently a similar use case as NLTemplate :-)

https://github.com/hughperkins/Jinja2CppLight

  • Jinja2-like syntax
  • lightweight, no dependencies on boost, qt, etc, ...
  • variable substitution
  • for loops
    • including nested for loops :-)
Hugh Perkins
  • 7,975
  • 7
  • 63
  • 71
  • By the way, I also made a different templater, based on an embedded lua engine. I think the lua version is quite cool, since lua is tiny, and fast, and the full power of lua is then at our disposal in the templates github.com/hughperkins/luacpptemplater – Hugh Perkins Jun 02 '16 at 12:41
  • 1
    It does not work at all. Even the examples in the README. – makerj Apr 11 '17 at 08:59
1

I developed something here modeled after jade for c++. It takes a simple but powerful input language and creates a single c++ template function that writes HTML to a stream.

< html
  < h1 The title is ${{ params["title"] }}& >
    < ul >
    & for(int i = 0; i < boost::get<int>(params["items"]); ++i) {
      < li Item ${{ i }}$ >
    & }
>
  • Variable replacement
  • User defined code blocks
  • harvests full power of c++ (loops, variable declarations, you name it)
  • Super easy to integrate into source builds
  • Everything possible done at compile time
  • No intermediate format (straight c++)
  • Easy to debug (because c++ output)
  • No external dependencies
  • Super tiny less than 600 lines of c++
  • GPL licensed
burner
  • 323
  • 2
  • 10
1

Templtext is a small C++ text template processing library. It supports bash-like variables (%VAR or %{VAR}). But the main feature is a support of user defined functions. The library was created by me.

  • Template parsing
  • Variable replacement
  • User defined functions in template
  • C++11
  • GPL license

need BOOST regex library, but it will be replaced with std::regex in the next version

Example 1:

using namespace templtext;

Templ * t = new Templ( "Dear %SALUTATION %NAME. I would like to invite you for %TEXT. Sincerely yours, %MYNAME." );

std::map<std::string, std::string> tokens01 =
{
        { "SALUTATION", "Mr." },
        { "NAME", "John Doe" },
        { "TEXT", "an interview" },
        { "MYNAME", "Ty Coon" }
};

std::map<std::string, std::string> tokens02 =
{
        { "SALUTATION", "Sweetheart" },
        { "NAME", "Mary" },
        { "TEXT", "a cup of coffee" },
        { "MYNAME", "Bob" }
};

std::cout << t->format( tokens01 ) << std::endl;
std::cout << t->format( tokens02 ) << std::endl;

Output:

Dear Mr. John Doe. I would like to invite you for an interview. Sincerely yours, Ty Coon.
Dear Sweetheart Mary. I would like to invite you for a cup of coffee. Sincerely yours, Bob.

Example 2:

using namespace templtext;

std::unique_ptr<Templ> tf1( new Templ( "You have got an $decode( 1 )." ) );
std::unique_ptr<Templ> tf2( new Templ( "You have got an $decode( 2 )." ) );
std::unique_ptr<Templ> tf3( new Templ( "English version - $decode_loc( 1, EN )." ) );
std::unique_ptr<Templ> tf4( new Templ( "German version  - $decode_loc( 1, DE )." ) );
std::unique_ptr<Templ> tf5( new Templ( "Flexible version - $decode_loc( 1, %LANG )." ) );

tf1->set_func_proc( func );
tf2->set_func_proc( func );
tf3->set_func_proc( func );
tf4->set_func_proc( func );
tf5->set_func_proc( func );

Templ::MapKeyValue map1 =
{
        { "LANG", "EN" }
};

Templ::MapKeyValue map2 =
{
        { "LANG", "DE" }
};

std::cout << tf1->format() << std::endl;
std::cout << tf2->format() << std::endl;
std::cout << tf3->format() << std::endl;
std::cout << tf4->format() << std::endl;
std::cout << tf5->format( map1 ) << std::endl;
std::cout << tf5->format( map2 ) << std::endl;

Output:

You have got an apple.
You have got an orange.
English version - apple.
German version  - Apfel.
Flexible version - apple.
Flexible version - Apfel.
trodevel
  • 143
  • 1
  • 5
1

Jinja2C++

Description:

  • C++14/17 library
  • Supports mainstream compilers (Visual C++, gcc, clang)
  • Easy-to-use interface.
  • Conformance to Jinja2 specification http://jinja.pocoo.org/docs/2.10/
  • Support for both narrow- and wide-character strings both for templates and - parameters.
  • Built-in reflection for C++ types and popular json libraries (nlohmann, RapidJson).
  • User-defined callables.
  • Powerful full-featured Jinja2 expressions with filtering (via ‘|’ operator) and ‘if’-expressions.
  • Big set of Jinja2 tags include macros and template extensions.
  • Rich error reporting.
1

I have tried using template engine and Dynamic C++ Pages provided by the ffead-cpp framework, an example implementation is shown on the wiki

Mark
  • 11
  • 1