Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/robertknight/qt-mustache
Mustache templating library for C++ using Qt
https://github.com/robertknight/qt-mustache
Last synced: 7 days ago
JSON representation
Mustache templating library for C++ using Qt
- Host: GitHub
- URL: https://github.com/robertknight/qt-mustache
- Owner: robertknight
- Created: 2012-08-27T11:16:35.000Z (about 12 years ago)
- Default Branch: master
- Last Pushed: 2023-07-19T08:49:43.000Z (over 1 year ago)
- Last Synced: 2024-10-25T13:34:09.124Z (14 days ago)
- Language: C++
- Size: 122 KB
- Stars: 84
- Watchers: 15
- Forks: 28
- Open Issues: 7
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
- awesome-qt - qt-mustache - mustache) - Render [Mustache](https://mustache.github.io) templates. (Libraries / New Functionality)
README
# Qt Mustache
qt-mustache is a simple library for rendering [Mustache templates](https://mustache.github.io/).
### Example Usage
```cpp
#include "mustache.h"QVariantHash contact;
contact["name"] = "John Smith";
contact["email"] = "[email protected]";QString contactTemplate = "{{name}} {{email}}";
Mustache::Renderer renderer;
Mustache::QtVariantContext context(contact);QTextStream output(stdout);
output << renderer.render(contactTemplate, &context);
```Outputs: `John Smith [email protected]`
For further examples, see the tests in `test_mustache.cpp`
### Building
* To build the tests, run `qmake` followed by `make`
* To use qt-mustache in your project, just add the `mustache.h` and `mustache.cpp` files to your project.
### License
qt-mustache is licensed under the BSD license.### Dependencies
qt-mustache depends on the QtCore library. It is compatible with Qt 5 and Qt 6.
## Usage### Syntax
qt-mustache uses the standard Mustache syntax. See the [Mustache manual](https://mustache.github.io/mustache.5.html) for details.
### Data Sources
qt-mustache expands Mustache tags using values from a `Mustache::Context`. `Mustache::QtVariantContext` is a simple
context implementation which wraps a `QVariantHash` or `QVariantMap`. If you want to render a template using a custom data source,
you can either create a `QVariantHash` which mirrors the data source or you can re-implement `Mustache::Context`.### Partials
When a `{{>partial}}` Mustache tag is encountered, qt-mustache will attempt to load the partial using a `Mustache::PartialResolver`
provided by the context. `Mustache::PartialMap` is a simple resolver which takes a `QHash` map of partial names
to values and looks up partials in that map. `Mustache::PartialFileLoader` is another simple resolver which
fetches partials from `.mustache` files in a specified directory.You can re-implement the `Mustache::PartialResolver` interface if you want to load partials from a custom source
(eg. a database).### Error Handling
If an error occurs when rendering a template, `Mustache::Renderer::errorPosition()` is set to non-negative value and
template rendering stops. If the error occurs whilst rendering a partial template, `errorPartial()` contains the name
of the partial.### Lambdas
The [Mustache manual](https://mustache.github.io/mustache.5.html) provides a mechanism to customize rendering of
template sections by setting the value for a tag to a callable object (eg. a lambda in Ruby or Javascript),
which takes the unrendered block of text for a template section and renders it itself. qt-mustache supports
this via the `Context::canEval()` and `Context::eval()` methods.