C++ HTML template framework, templatizing library, HTML generator library [closed]
Asked Answered
A

13

65

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.

Attalie answered 10/12, 2008 at 10:41 Comment(2)
far too many are obsolete, not updated, or dead links.Lion
First answer is community wiki, you can update it.Attalie
D
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++

Dialectology answered 10/12, 2008 at 10:42 Comment(1)
Almost all of these are dead links now. Lost in time, like tears in rain...Spavined
R
8

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

Roundish answered 3/3, 2010 at 13:58 Comment(1)
@gbjbaanb: I have updated the url to the new location.Roundish
M
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.

Madea answered 7/4, 2013 at 19:38 Comment(3)
Like it for being single-file. It's a bit dated though, last commit was 2 years ago.Lindell
NLTemplate is pretty good, thanks CatNapGames! There were a few things I didn’t like about it (throwing strings instead of exceptions, lack of nil-type block references), so I wrote something similar. github.com/michaelainsworth/StringTemplateHanshansard
The above link appears to be dead.Salyer
S
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.

Siclari answered 11/2, 2009 at 17:26 Comment(2)
Worth remembering, however I was looking on how to embed HTML templatizing functionality into a C++ desktop program.Attalie
I'd definitely call it a framework. It even generates JavaScript code. See webtoolkit.eu/wt/src/helloPurkey
C
3

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

Cimbura answered 2/4, 2009 at 11:20 Comment(1)
The documentation is out of date and it appears development has stalledCranny
S
2

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

Shellfish answered 10/12, 2008 at 11:17 Comment(0)
R
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"
Retsina answered 23/3, 2013 at 21:28 Comment(0)
B
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

Bladderwort answered 1/6, 2011 at 4:28 Comment(0)
D
1

ctemplate

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

New BSD License

Decumbent answered 21/6, 2012 at 13:3 Comment(0)
S
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 :-)
Strangles answered 20/2, 2015 at 9:21 Comment(2)
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/luacpptemplaterStrangles
It does not work at all. Even the examples in the README.Seeker
F
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
Federative answered 16/5, 2015 at 23:23 Comment(0)
N
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.
Northnortheast answered 9/8, 2015 at 22:33 Comment(0)
C
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.
Confessional answered 20/6, 2019 at 18:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.