问题
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.
回答1:
A quick review of the mentioned project.
http://rgrz.tumblr.com/post/13808947359/review-of-html-template-engines-in-c-language
ClearSilver
- Site: http://www.clearsilver.net
- Project: https://code.google.com/p/clearsilver/
- Group: http://tech.groups.yahoo.com/group/ClearSilver
- License: New BSD License
- Language: C
- Last Update: Nov 28, 2011
- Last Release: 0.10.5 on July 12, 2007
- Document: Rich
- Community: Medium (<10 discussion per month)
Teng
- Site: http://teng.sourceforge.net
- Code: http://teng.svn.sourceforge.net/teng/
- Group: http://sourceforge.net/projects/teng/
- License: New BSD License
- Language: C++
- Binding: php, python
- Last Update: Mar 8, 2011
- Last Release: 2.1.1 on Mar 8, 2011
- Document: Rich
- Community: Low (rare discussion since 2010)
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++
- Site: http://nulidex.com/code/docs/html_template/
- Project: http://sourceforge.net/projects/htmltemplatec
- Group: http://sourceforge.net/projects/htmltemplatec
- License: GPL
- Language: C++
- Last Update: Mar 27, 2011
- Last Release: Beta 0.7.4, Mar 27, 2011
- Document: Medium
- Community: none
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
- Site: http://www.webtoolkit.eu/wt/
- Project: http://www.webtoolkit.eu/wt/
- Group: http://www.webtoolkit.eu/wt/community
- License: GPL and Commercial
- Language: C++
- Last Update: Nov 29, 2011
- Last Release: 3.2.0 on Nov 29, 2011
- Document: Rich
- Community: Low (rare activity)
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++
- Site: https://jinja2cpp.dev
- Project: https://github.com/jinja2cpp
- Group: https://gitter.im/Jinja2Cpp/Lobby
- Conan packages: https://bintray.com/beta/#/flexferrum/conan-packages/jinja2cpp:flexferrum?tab=overview
- License: MPL-2.0
- Language: C++14/17
- Last Update: Oct 01, 2019
- Last Release: 1.0.0 on Oct 01, 2019
- Document: Moderate
- Community: none
回答2:
Grantlee is a string template engine based on the Django template system. It is ported to C++/Qt.
回答3:
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.
回答4:
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.
回答5:
CTPP is very fast and powerful library written in C++. It has bindings for Perl, PHP and Python.
回答6:
ClearSilver is available for c. Here is a list of existing websites which use clearsilver. But I don't use it myself.
回答7:
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"
回答8:
I have tried using template engine and Dynamic C++ Pages provided by the ffead-cpp framework, an example implementation is shown on the wiki
回答9:
ctemplate
https://code.google.com/p/ctemplate/?redir=1
New BSD License
回答10:
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 :-)
回答11:
Jinja2C++
- Site: https://jinja2cpp.dev
- Project: https://github.com/jinja2cpp
- Conan package: https://bintray.com/beta/#/flexferrum/conan-packages/jinja2cpp:flexferrum?tab=overview
- License: MPL-2.0
- Language: C++14/17
- Last Update: Oct 01, 2019
- Last Release: 1.0.0 on Oct 01, 2019
- Document: Moderate
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.
回答12:
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
回答13:
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.
来源:https://stackoverflow.com/questions/355650/c-html-template-framework-templatizing-library-html-generator-library