|
Nuria Framework - Core
Core module of the NuriaProject Framework
|
#include <templateengine.hpp>
Public Member Functions | |
| TemplateEngine (const QString &templData=QString()) | |
| TemplateEngine (QIODevice *readDevice) | |
| QString | templateData () const |
| void | bind (const QString &name, const QVariant &data) |
| void | bind (const QString &name, QObject *object) |
| void | unbind (const QString &name) |
| void | clearBindings () |
| void | setTemplateData (const QString &templData) |
| QString | generate () |
Static Public Member Functions | |
| static QString | generate (const QString &templateData, const QVariantMap &variables) |
This class provides a simple to use templating engine for text data.
A bound variable may be of any type that can be converted to a QString. Also supported are lists and maps which are registered to the Nuria::Variant system. Variable names may consist of the following characters: a-z, 0-9, "_", "-" and ".". Names are always matched case-sensitive!
To access the first or last item in a list, use "first" or "last" respectively: <=List.first> ... <=List.last>
To access an item at a random index, use "random": <=List.random>
true. A modifier always begins with "%" instead of "=" to be easily distinguishable. Most modifiers can have an "<%else>" block which are evaluated when the statement turned out to be false. A block is always ended with "<%%>". The modifiers which can have an else block can also be negated using the negation operator "!" written before the modifier itself. For example, you have a bound QObject "object" with a slot named "users()" which when called returns a list of strings. You can output all elements as you'd expect using Each: <Each:object.users()><=Value><%%>
Beside these two special variables, various special modifiers are provided:
| Nuria::TemplateEngine::TemplateEngine | ( | const QString & | templData = QString() | ) |
Constructs a template engine instance with templateData set to templData.
| Nuria::TemplateEngine::TemplateEngine | ( | QIODevice * | readDevice | ) |
Constructs a template engine instance. The templateData is read from readDevice. Data is interpreted as UTF-8. The readDevice must be open and readable. All data will be read out of it. The device won't be closed afterwards and ownership will remain at the caller.
| void Nuria::TemplateEngine::bind | ( | const QString & | name, |
| const QVariant & | data | ||
| ) |
Binds variable name to data.
| void Nuria::TemplateEngine::bind | ( | const QString & | name, |
| QObject * | object | ||
| ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts. Convenience overload for binding QObjects.
| void Nuria::TemplateEngine::clearBindings | ( | ) |
Clears the list of bound variables.
| QString Nuria::TemplateEngine::generate | ( | ) |
Generates the result of the template.
|
static |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
| void Nuria::TemplateEngine::setTemplateData | ( | const QString & | templData | ) |
Sets the template data.
| QString Nuria::TemplateEngine::templateData | ( | ) | const |
Returns the current template data. Is empty by default.
| void Nuria::TemplateEngine::unbind | ( | const QString & | name | ) |
Removes the variable name.
1.8.7