Download as a zip archive or clone a repository:
git clone https://github.com/YuriyLisovskiy/TemplateRender.git
Move to the project folder:
cd TemplateRender
-
Include
src/include/Header.hto a file whereTemplateRender::render()is used. -
In
src/include/Config.hfile specify the pathsTEMPLATE_DIRfor template search,ENDPOINT_DIRfor rendered HTML document andMEDIA_DIRfor media files search.Default values which are used for testing:
TEMPLATE_DIR: "ROOT_DIR/test/templates/" ENDPOINT_DIR: "ROOT_DIR/test/" MEDIA_DIR: "ROOT_DIR/test/media/" -
Create a context object using vector of pairs of keys and values (or do not create if it is not used).
* Keys and values must have
std::stringtype, useTemplateRender::str()to convert any data to string.* Required an
std::ostringstreamoperator for custom data types.* For arrays of objects use
std::vectorcontainer.Example:
std::vector<std::pair<std::string, std::string>> context = { { "first_key", "first_value" }, { "second_key", "second_value" }, { "third_key", TemplateRender::str<int>(2017) } }; Context* contextObject = new Context(context); -
Design a template (check for 'Available syntax' section).
-
In
AppStart.cppcallTemplateRender::render()function and pass arguments: the first is template name, the second is rendered HTML document name, the third is your context (if context is not used ignore this argument, default isnullptr).Example:
TemplateRender::render("index.html", "completed.html", contextObject); -
Build and run the project.
-
Find rendered HTML document in the
ENDPOINT_DIRdirectory that was specified earlier.
-
'For' loop statement tag.
Example:
{% for (size_t i = 0; i < 5; i++) %} /*loop_body*/ {{ i }} /*loop_body*/ {% endfor %} -
'Foreach' loop statement tag.
Example:
{% for (auto some_var : some_collection) %} /*loop_body*/ {{ some_var }} /*loop_body*/ {% endfor %} -
'If' statement tag.
Example:
{% if (some_var_from_context) %} /*condition_body*/ {% else %} /*else_body*/ {% endif %}{% if (first_var_from_context <= second_var_from_context) %} /*condition_body*/ {% endif %}* Only a comparison of two strings or two numbers is available.
-
Tag for including another html documents.
Example:
{% #include "some_snippet.html" %} -
Tag for commenting code in templates.
Example:
{% comment "Comment explanation" %} /*Some content to comment on*/ {% endcomment %} -
Variables.
Example:
{{ some_var_from_context }} -
Static files.
Example:
<img src="{% #static 'some_image_from_context' %}" /><img src="{% #static 'some_image.png' %}" />
Note: the project is not tested completely, so, there still can be bugs.
- Yuriy Lisovskiy - the majority of the project
- Yuriy Vasko
- Andrii Vaskiv
- Andriy Dubyk
- Natalia Pachva
- Oleg Zhuk