Workspace 6.21.5
Public Types | Public Member Functions | Protected Member Functions | List of all members
TextTemplate Class Reference

Represents a template (or set of templates) that can be rendered using a supplied context. More...

#include <Workspace/Application/texttemplate.h>

Public Types

using RenderContext = QVariantMap
 
using TokenSet = QSet< QString >
 

Public Member Functions

 TextTemplate (const QString &location, const QStringList &fileNames)
 
 TextTemplate (const QString &templateText)
 
 TextTemplate (const QStringList &templateTexts)
 
QStringList getErrors () const
 
const QString & getListJoinToken () const
 
QStringList render (const RenderContext &context)
 
void setListJoinToken (const QString &token)
 

Protected Member Functions

 TextTemplate ()
 

Detailed Description

Doesn't do anything fancy - no logic is allowed in the templates - it just does a simple replacement of all placeholder tokens using the supplied context. Placeholders are identified by being surrounded by {% %} characters, e.g:

{% Placeholder %}

Placeholder tokens can be escaped with a single backslash. For example:

\{% Placeholder %}

Will cause the text, "{% Placeholder %}" to be rendered, as the placeholder will be ignored.

The context is a map of token name -> qvariant, where the variant can be any of:

Elements in lists (e.g. ObjectArrays) can be accessed using the usual index notation. For example:

{% Placeholder[0] %}

Will access the 0th element of the array Placeholder in the context passed to the render() method. Elements in maps (e.g. ObjectDictionaries) can be accessed using the expected dot notations. For example:

{% Placeholder.Member %}

Will access the Member member of the Placeholder dictionary.

Member Typedef Documentation

◆ RenderContext

using RenderContext = QVariantMap

◆ TokenSet

using TokenSet = QSet<QString>

Constructor & Destructor Documentation

◆ TextTemplate() [1/4]

TextTemplate ( const QString &  location,
const QStringList fileNames 
)

◆ TextTemplate() [2/4]

TextTemplate ( const QStringList templateTexts)

◆ TextTemplate() [3/4]

TextTemplate ( const QString &  templateText)

◆ TextTemplate() [4/4]

TextTemplate ( )
protected

Member Function Documentation

◆ getErrors()

QStringList getErrors ( ) const
inline

◆ getListJoinToken()

const QString & getListJoinToken ( ) const
Returns
the token to be used to join lists of elements together.

◆ render()

QStringList render ( const RenderContext context)
Parameters
contextThe context full of name variables to substitute into the template.
Returns
A list of output strings, one for each rendered input template. If the single template text constructor was used, this list will contain only a single output string.

◆ setListJoinToken()

void setListJoinToken ( const QString &  token)
Parameters
tokenThe token to be used to join lists of elements together.