C++ HTML template framework, templatizing library, HTML generator library

C++Template Engine

C++ Problem Overview


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.

C++ Solutions


Solution 1 - C++

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

Solution 2 - C++

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

Solution 3 - C++

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.

Solution 4 - C++

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.

Solution 5 - C++

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

Solution 6 - C++

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

Solution 7 - C++

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"

Solution 8 - C++

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

Solution 9 - C++

Solution 10 - C++

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

Solution 11 - C++

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

Solution 12 - C++

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.

Solution 13 - C++

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMarcin GilView Question on Stackoverflow
Solution 1 - C++Marcin GilView Answer on Stackoverflow
Solution 2 - C++KristianView Answer on Stackoverflow
Solution 3 - C++Tomas AndrleView Answer on Stackoverflow
Solution 4 - C++yesraajView Answer on Stackoverflow
Solution 5 - C++villie2View Answer on Stackoverflow
Solution 6 - C++wimhView Answer on Stackoverflow
Solution 7 - C++kirill_igumView Answer on Stackoverflow
Solution 8 - C++MarkView Answer on Stackoverflow
Solution 9 - C++OJWView Answer on Stackoverflow
Solution 10 - C++Hugh PerkinsView Answer on Stackoverflow
Solution 11 - C++burnerView Answer on Stackoverflow
Solution 12 - C++trodevelView Answer on Stackoverflow
Solution 13 - C++Sergei SadovnikovView Answer on Stackoverflow