Stellarium
0.20.4
|
Enhanced version of QString for template processing. More...
#include <template.h>
Public Member Functions | |
Template (QString source, QString sourceName) | |
Constructor that reads the template from a string. More... | |
Template (QFile &file, QTextCodec *textCodec=Q_NULLPTR) | |
Constructor that reads the template from a file. More... | |
int | setVariable (QString name, QString value) |
Replace a variable by the given value. More... | |
int | setCondition (QString name, bool value) |
Set a condition. More... | |
int | loop (QString name, int repetitions) |
Set number of repetitions of a loop. More... | |
void | enableWarnings (bool enable=true) |
Enable warnings for missing tags. More... | |
void | translate (ITemplateTranslationProvider &provider) |
Translates all translation tags using the given translation provider. More... | |
Templates are usually loaded from files, but may also be loaded from prepared Strings. Example template file:
Hello {username}, how are you?
{if locked} Your account is locked. {else locked} Welcome on our system. {end locked}
The following users are on-line: Username Time {loop user} {user.name} {user.time} {end user}
Example code to fill this template:
Template t(QFile("test.tpl"),QTextCode::codecForName("UTF-8")); t.setVariable("username", "Stefan"); t.setCondition("locked",false); t.loop("user",2); t.setVariable("user0.name,"Markus"); t.setVariable("user0.time,"8:30"); t.setVariable("user1.name,"Roland"); t.setVariable("user1.time,"8:45");
The code example above shows how variable within loops are numbered. Counting starts with 0. Loops can be nested, for example:
<table> {loop row} <tr> {loop row.column} <td>{row.column.value}</td> {end row.column} </tr> {end row} </table>
Example code to fill this nested loop with 3 rows and 4 columns:
t.loop("row",3);
t.loop("row0.column",4); t.setVariable("row0.column0.value","a"); t.setVariable("row0.column1.value","b"); t.setVariable("row0.column2.value","c"); t.setVariable("row0.column3.value","d");
t.loop("row1.column",4); t.setVariable("row1.column0.value","e"); t.setVariable("row1.column1.value","f"); t.setVariable("row1.column2.value","g"); t.setVariable("row1.column3.value","h");
t.loop("row2.column",4); t.setVariable("row2.column0.value","i"); t.setVariable("row2.column1.value","j"); t.setVariable("row2.column2.value","k"); t.setVariable("row2.column3.value","l");
Template::Template | ( | QString | source, |
QString | sourceName | ||
) |
source | The template source text |
sourceName | Name of the source file, used for logging |
Template::Template | ( | QFile & | file, |
QTextCodec * | textCodec = Q_NULLPTR |
||
) |
Note that this class does not cache template files by itself, so using this constructor is only recommended to be used on local filesystem.
file | File that provides the source text |
textCodec | Encoding of the source. If null, UTF-8 is assumed. |
void Template::enableWarnings | ( | bool | enable = true | ) |
enable | Warnings are enabled, if true |
int Template::loop | ( | QString | name, |
int | repetitions | ||
) |
This affects tags with the syntax
name | Name of the loop |
repetitions | The number of repetitions |
int Template::setCondition | ( | QString | name, |
bool | value | ||
) |
This affects tags with the syntax
name | Name of the condition |
value | Value of the condition |
int Template::setVariable | ( | QString | name, |
QString | value | ||
) |
Affects tags with the syntax
After settings the value of a variable, the variable does not exist anymore, it it cannot be changed multiple times.
name | name of the variable |
value | new value |
void Template::translate | ( | ITemplateTranslationProvider & | provider | ) |
The translation tags have a PHP-like format to be able to be parsed using gettext/xgettext. Examples: <?= tr("this is an example") ?>, <?= tr("You have %1 new messages", "20") ?>