项目作者: sohaibiftikhar

项目描述 :
Simple templates is a templating library written entirely in C++.
高级语言: C++
项目地址: git://github.com/sohaibiftikhar/simpletemplates.git
创建时间: 2019-10-12T19:12:29Z
项目社区:https://github.com/sohaibiftikhar/simpletemplates

开源协议:

下载


Simple Templates

Simple templates is a templating library written entirely in C++.

Installation

Simple templates uses cmake as a build system. For cmake projects
it is better to have an out-of-source
build tree.

The code uses C++ 14 as a standard and has no external dependencies.

```shell script
export CXX=g++-9
mkdir build
cd build
cmake —configure ..

For building the library and the tests you can build all targets

../build_all

  1. This should create an executable `engine_test`. Run it to check if the library compilation
  2. was a success. The results from the test would look something like this.
  3. ![Alt text](docs/tests.png)
  4. Other than the executable cmake should also have created
  5. a static library `libsimple_templates.a` which you can link to your own project.
  6. #### User Documentation
  7. ###### Usage
  8. Simple templates works with streams. A sample code for usage and its output
  9. is given below.
  10. ```c++
  11. #include <iostream>
  12. #include <string>
  13. #include <sstream>
  14. #include "TemplateEngine.hpp"
  15. #include "Template.hpp"
  16. #include "Renderable.hpp"
  17. #include "TemplateConfig.hpp"
  18. using namespace std;
  19. int main() {
  20. std::stringstream istream("Hello {{user}}! Welcome to Simple Templates!");
  21. std::stringstream outstream;
  22. TemplateConfig myConfig("{{", "}}");
  23. TemplateEngine myEngine;
  24. Template myTemplate = myEngine.compile("WelcomeTemplate", istream, myConfig);
  25. std::map<std::string, std::unique_ptr<Renderable>> myContext;
  26. myContext.insert({"user", std::make_unique<StringRenderable>("Suhaib")});
  27. myTemplate.bind(outstream, myContext);
  28. std::cout<<outstream.str()<<std::endl;
  29. }

Lets we call this file test.cpp. You can compile this as follows:

```shell script
g++-9 —std=c++14 -I/includes -c test.cpp -o test.o

  1. Then link it against the compiled library we produced above.
  2. ```shell script
  3. g++-9 test.o -L <path_to_simple_templates>/build -lsimple_templates -o test.exe

And finally you can run it
```shell script
./test.exe
Hello Suhaib! Welcome to Simple Templates!

  1. ###### Template Configuration
  2. Template start and end expressions are defined through the `TemplateConfig`.
  3. The start and end expression strings must be non-zero length and can contain
  4. the following characters `{[(<|$>)]}`.
  5. ###### Context
  6. The context map is an `std::map`. The following type of
  7. types are supported.
  8. * `StringRenderable`: For simple strings.
  9. * `IntRenderable`: For simple integers.
  10. * `FloatRenderable`: For simple floating point numbers.
  11. * `BoolRenderable`: For simple booleans.
  12. * `ListRenderable<Renderable>`: For an array of any of the simple types or
  13. another list.
  14. Important: Contexts are not reusable. The data inside a context could have been moved
  15. after calling `Template::bind`.
  16. ###### Expressions
  17. The template supports three types of expressions. Unless otherwise mentioned
  18. `{{` and `}}` are used as the expression-start and expression-end symbol throughout
  19. this documentation.
  20. * Simple: Any variable within the `{{` and `}}`. Variable names must
  21. start with an alphabet and can only contain alphabets, numbers and '_'.
  22. * Conditional: Following is an example of a conditional expression.
  23. ```mustache
  24. {{#if conditionalVar}}
  25. Do something here
  26. {{/if}}

The conditionalVar must be a valid variable name that maps to a
BoolRenderable in the context when binding.

  • Loops: Following is an example of a loop.
    1. {{#loop listVar loopVar}}
    2. Do something here with {{loopVar}}
    3. {{/loop}}
    The listVar must be a valid variable name that maps to a
    ListRenderable in the context when binding. Since the context is global,
    the loopVar must not collide with any other variable in the context
    including other loop variables.

You can also nest loops within loops or loops within conditions and so on.