Skip to content

Commit 775ecf1

Browse files
committed
Update ReadMe.md
1 parent 36e0a71 commit 775ecf1

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed

README.md

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,109 @@ Helloworld!!!
3636
Hello, world!!!
3737
Hello; world!!!
3838
```
39+
40+
# Getting started
41+
42+
In order to use Jinja2Cpp in your project you have to:
43+
* Clone the Jinja2Cpp repository
44+
* Build it according with the instructions
45+
* Link with your project.
46+
47+
Usage of Jinja2Cpp in the code is pretty simple:
48+
1. Declare the jinja2::Template object:
49+
50+
```c++
51+
jinja2::Template tpl;
52+
```
53+
54+
2. Populate it with template:
55+
56+
```c++
57+
tpl.Load("{{'Hello World' }}!!!");
58+
```
59+
60+
3. Render the template:
61+
62+
```c++
63+
std::cout << tpl.RenderAsString(jinja2::ValuesMap{}) << std::endl;
64+
```
65+
66+
and get:
67+
68+
`
69+
Hello World!!!
70+
`
71+
72+
That's all!
73+
74+
The render procedure is stateless, so you can perform several renderings simultaneously in different threads. Even if you pass parameters:
75+
76+
```c++
77+
ValuesMap params = {
78+
{"intValue", 3},
79+
{"doubleValue", 12.123f},
80+
{"stringValue", "rain"},
81+
{"boolFalseValue", false},
82+
{"boolTrueValue", true},
83+
};
84+
85+
std::string result = tpl.RenderAsString(params);
86+
std::cout << result << std::endl;
87+
```
88+
89+
Parameters could have the following types:
90+
- std::string/std::wstring
91+
- integer (int64_t)
92+
- double
93+
- boolean (bool)
94+
- Tuples (also known as arrays)
95+
- Dictionaries (also known as maps)
96+
97+
Tuples and dictionaries can be mapped to the C++ types. So you can smoothly reflect your structures and collections into the template engine:
98+
99+
```c++
100+
namespace jinja2
101+
{
102+
template<>
103+
struct TypeReflection<reflection::EnumInfo> : TypeReflected<reflection::EnumInfo>
104+
{
105+
static auto& GetAccessors()
106+
{
107+
static std::unordered_map<std::string, FieldAccessor> accessors = {
108+
{"name", [](const reflection::EnumInfo& obj) {return Reflect(obj.name);}},
109+
{"scopeSpecifier", [](const reflection::EnumInfo& obj) {return Reflect(obj.scopeSpecifier);}},
110+
{"namespaceQualifier", [](const reflection::EnumInfo& obj) { return obj.namespaceQualifier;}},
111+
{"isScoped", [](const reflection::EnumInfo& obj) {return obj.isScoped;}},
112+
{"items", [](const reflection::EnumInfo& obj) {return Reflect(obj.items);}},
113+
};
114+
115+
return accessors;
116+
}
117+
};
118+
119+
// ...
120+
jinja2::ValuesMap params = {
121+
{"enum", jinja2::Reflect(enumInfo)},
122+
};
123+
```
124+
125+
In this cases method 'jinja2::reflect' reflects regular C++ type into jinja2 template param. If type is a user-defined class or structure then handwritten mapper 'TypeReflection<>' should be provided.
126+
127+
# Current Jinja2 support
128+
Currently, Jinja2Cpp supports the limited number of Jinja2 features. By the way, Jinja2Cpp is planned to be full [jinja2 specification](http://jinja.pocoo.org/docs/2.10/templates/)-conformant. The current support is limited to:
129+
- expressions. You can use almost every style of expressions: simple, filtered, conditional, and so on.
130+
- limited number of filters (**join**, **sort**)
131+
- limited number of testers (**defined**, **startsWith**)
132+
- limited number of functions (**range**, **loop.cycle**)
133+
- 'if' statement (with 'elif' and 'else' branches)
134+
- 'for' statement (with 'else' branch support)
135+
- 'set' statement
136+
137+
# Supported compilers
138+
Compilation of Jinja2Cpp tested on the following compilers (with C++14 enabled feature):
139+
- Linux gcc 5.0
140+
- Linux gcc 6.0
141+
- Linux gcc 7.0
142+
- Linux clang 5.0
143+
- Microsoft Visual Studio 2015 x86
144+
- Microsoft Visual Studio 2017 x86

0 commit comments

Comments
 (0)