-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Martin Ribelotta <[email protected]>
1 parent
0290c47
commit 669791b
Showing
36 changed files
with
3,476 additions
and
104 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
build-debug |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
language: cpp | ||
|
||
env: | ||
- QT_SELECT=qt4 | ||
- QT_SELECT=qt5 | ||
|
||
before_install: | ||
- sudo add-apt-repository --yes ppa:ubuntu-sdk-team/ppa | ||
- sudo apt-get update -qq | ||
- sudo apt-get install -qq libqtcore4 qt4-qmake libqt5core5a qt5-qmake qt5-default qtchooser | ||
|
||
script: | ||
- qmake && make && ./qt-mustache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
[![Build Status](https://travis-ci.org/robertknight/qt-mustache.svg?branch=master)](https://travis-ci.org/robertknight/qt-mustache) | ||
|
||
# Qt Mustache | ||
|
||
qt-mustache is a simple library for rendering [Mustache templates](http://mustache.github.com/). | ||
|
||
### Example Usage | ||
|
||
```cpp | ||
#include "mustache.h" | ||
|
||
QVariantHash contact; | ||
contact["name"] = "John Smith"; | ||
contact["email"] = "[email protected]"; | ||
|
||
QString contactTemplate = "<b>{{name}}</b> <a href=\"mailto:{{email}}\">{{email}}</a>"; | ||
|
||
Mustache::Renderer renderer; | ||
Mustache::QtVariantContext context(contact); | ||
|
||
QTextStream output(stdout); | ||
output << renderer.render(contactTemplate, &context); | ||
``` | ||
Outputs: `<b>John Smith</b> <a href="mailto:[email protected]">[email protected]</a>` | ||
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 4 and Qt 5. | ||
## Usage | ||
### Syntax | ||
qt-mustache uses the standard Mustache syntax. See the [Mustache manual](http://mustache.github.com/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<QString,QString>` map of partial names | ||
to values and looks up partials in that map. `Mustache::PartialFileLoader` is another simple resolver which | ||
fetches partials from `<partial name>.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](http://mustache.github.com/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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
INCLUDEPATH += $$PWD/src | ||
|
||
HEADERS += $$PWD/src/mustache.h | ||
SOURCES += $$PWD/src/mustache.cpp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
###################################################################### | ||
# Automatically generated by qmake (2.01a) Mon Aug 27 11:20:05 2012 | ||
###################################################################### | ||
|
||
TEMPLATE = app | ||
DEPENDPATH += . src tests | ||
INCLUDEPATH += . src tests | ||
QT += testlib | ||
QT -= gui | ||
CONFIG -= app_bundle | ||
|
||
!win32 { | ||
QMAKE_CXXFLAGS += -Werror -Wall -Wextra -Wnon-virtual-dtor | ||
} | ||
|
||
# Input | ||
HEADERS += src/mustache.h tests/test_mustache.h | ||
SOURCES += src/mustache.cpp tests/test_mustache.cpp | ||
|
||
# Copies the given files to the destination directory | ||
defineTest(copyToDestdir) { | ||
files = $$1 | ||
|
||
for(FILE, files) { | ||
DDIR = $$OUT_PWD | ||
|
||
# Replace slashes in paths with backslashes for Windows | ||
win32:FILE ~= s,/,\\,g | ||
win32:DDIR ~= s,/,\\,g | ||
|
||
QMAKE_POST_LINK += $$QMAKE_COPY $$quote($$FILE) $$quote($$DDIR) $$escape_expand(\\n\\t) | ||
} | ||
|
||
export(QMAKE_POST_LINK) | ||
} | ||
|
||
copyToDestdir($$PWD/tests/*.mustache) | ||
copyToDestdir($$PWD/tests/specs/*.json) |
Oops, something went wrong.