QtWebApp
stefanfrings::Template Class Reference

Enhanced version of QString for template processing. More...

#include <template.h>

Inheritance diagram for stefanfrings::Template:
Collaboration diagram for stefanfrings::Template:

Public Member Functions

 Template (const QString source, const QString sourceName)
 Constructor that reads the template from a string. More...
 
 Template (QFile &file, const QTextCodec *textCodec)
 Constructor that reads the template from a file. More...
 
int setVariable (const QString name, const QString value)
 Replace a variable by the given value. More...
 
int setCondition (const QString name, bool value)
 Set a condition. More...
 
int loop (QString name, const int repetitions)
 Set number of repetitions of a loop. More...
 
void enableWarnings (const bool enable=true)
 Enable warnings for missing tags. More...
 

Detailed Description

Enhanced version of QString for template processing.

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");

See also
TemplateLoader
TemplateCache

Definition at line 92 of file template.h.

Constructor & Destructor Documentation

◆ Template() [1/2]

Template::Template ( const QString  source,
const QString  sourceName 
)

Constructor that reads the template from a string.

Parameters
sourceThe template source text
sourceNameName of the source file, used for logging

Definition at line 11 of file template.cpp.

◆ Template() [2/2]

Template::Template ( QFile &  file,
const QTextCodec *  textCodec 
)

Constructor that reads the template from a file.

Note that this class does not cache template files by itself, so using this constructor is only recommended to be used on local filesystem.

Parameters
fileFile that provides the source text
textCodecEncoding of the source
See also
TemplateLoader
TemplateCache

Definition at line 18 of file template.cpp.

Member Function Documentation

◆ enableWarnings()

void Template::enableWarnings ( const bool  enable = true)

Enable warnings for missing tags.

Parameters
enableWarnings are enabled, if true

Definition at line 239 of file template.cpp.

◆ loop()

int Template::loop ( QString  name,
const int  repetitions 
)

Set number of repetitions of a loop.

This affects tags with the syntax

  • {loop name}...{end name}
  • {loop name}...{else name}...{end name}
Parameters
nameName of the loop
repetitionsThe number of repetitions
Returns
The number of loops that have been processed

Definition at line 155 of file template.cpp.

◆ setCondition()

int Template::setCondition ( const QString  name,
bool  value 
)

Set a condition.

This affects tags with the syntax

  • {if name}...{end name}
  • {if name}...{else name}...{end name}
  • {ifnot name}...{end name}
  • {ifnot name}...{else name}...{end name}
Parameters
nameName of the condition
valueValue of the condition
Returns
The count of conditions that have been processed

Definition at line 57 of file template.cpp.

◆ setVariable()

int Template::setVariable ( const QString  name,
const QString  value 
)

Replace a variable by the given value.

Affects tags with the syntax

  • {name}

After settings the value of a variable, the variable does not exist anymore, it it cannot be changed multiple times.

Parameters
namename of the variable
valuenew value
Returns
The count of variables that have been processed

Definition at line 39 of file template.cpp.


The documentation for this class was generated from the following files: