Question

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.

Was it helpful?

Solution

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++

OTHER TIPS

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

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.

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.

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

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

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"

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

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 :-)

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

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.

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/
  • Partial support for both narrow- and wide-character strings both for templates and - parameters.
  • Built-in reflection for C++ types.
  • 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.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top