1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
|
/*!
\page ktexttemplate-for-app-devs
\title KTextTemplate for application developers
Integrating KTextTemplate into applications is very simple. This page describes
\list
\li How to render a Template with a Context
\li How to load Templates
\li How to extend the syntax of KTextTemplate
\li Patterns of use of KTextTemplate
\endlist
If you are not already familiar with Django template syntax and structure, start with \l {KTextTemplate for theme artists}. If you are already familiar with Django, you might find \l {Differences between Django and KTextTemplate} informative.
\section1 Rendering Templates
\section2 Creating Templates
Rendering templates is very easy in application code. A single Template may be rendered multiple times with different Context objects.
\code
auto engine = new KTextTemplate::Engine( this );
auto t = engine->newTemplate("My name is {{ name }}.", "my_template_name");
QVariantHash mapping;
mapping.insert("name", "Grainne");
KTextTemplate::Context c(mapping);
t->render(&c); // Returns "My name is Grainne."
mapping.insert("name", "Henry");
c = KTextTemplate::Context(mapping);
t->render(&c); // Returns "My name is Henry."
\endcode
Usually, applications do not create a Template directly, but instead use a KTextTemplate::Engine to load external files. This allows artists to define the template without needing to recompile.
\code
auto t = engine->loadByName("template.html");
t->render(&c)
\endcode
\section2 Variables
A Context object maps a string to another object for reference in the template. String keys in the Context object are available as variables in the Template, and can be used with the \c variable syntax or inside \c control tags. In the above example, we mapped the string \c "name" to the string \c "Grainne" and then to the string \c "Henry". We can create more than just string mappings though.
\code
mapping.insert("myint", 6); // Can insert ints
mapping.insert("mydouble", 6.5); // Can insert doubles
mapping.insert("mybool", false); // Can insert bools
QVariantList mylist{"Ingrid", 3};
mapping.insert("mylist", mylist); // Can insert QVariantList
QVariantHash myhash;
myhash.insert("James", "Artist");
myhash.insert("Kiera", "Dreamer");
mapping.insert("myhash", myhash); // Can insert QVariantHash
QObject *obj = getObj();
auto objVar = QVariant::fromValue(obj);
mapping.insert("myobject", objVar); // Can insert QObject*
\endcode
It is additionally possible to insert any type of object or any container (not just QVariantHash and QVariantList) into the Context.
See \l {Generic type and template support}.
\section1 Extending KTextTemplate
KTextTemplate has 5 extension points.
\list
\li Custom object variables
\li Generic types and containers
\li Filters
\li Tags
\li Loaders
\endlist
\section2 Custom objects
Instances of QObject* can be inserted into templates. The recommended way to insert custom objects into templates is to create QObject wrappers for your objects. As QObject is introspectable, this will allow lookups to work in a way you define.
\note If you are already familiar with Django you will know that creating wrappers is not necessary in Django. That is because python objects are already fully introspectable.
\code
#include "myperson.h"
class PersonWrapper : public QObject
{
Q_OBJECT
Q_PROPERTY(QString name READ name)
public:
PersonWrapper(const QString &name, int age);
QString name() const;
int age() const;
QString clear();
};
...
auto linda = new PersonWrapper("Linda", 21);
mapping.insert("person", linda);
...
The name is {{ person.name }} and age is {{ person.age }}.
// Will be rendered as
// The name is Linda and age is .
\endcode
Note that the \c 'name' of \c PersonWrapper is accessible in the template, but the \c 'age' is not. Note also that rendering fails silently if the method can not be found. Only methods which have a corresponding \c Q_PROPERTY declaration are accessible from templates. To make \c age accessible in the template, we would need to add
\code
Q_PROPERTY(int age READ age)
\endcode
to the class. Note also that those methods are \c const. Rendering a template should never change an object it is rendering. Always make sure the READ properties of your wrapper objects are \c const. It is also possible to lookup dynamic properties of \c {QObject}s. In the case of dynamic properties, the property name must be UTF-8 encoded.
If you already have \c {QObject}s in your application which would be suitable for use in templates, you only need to add some \c Q_PROPERTY entries to the classes to make them accessible to KTextTemplate.
\note If you are familiar with Django you may be aware of the \c alters_data attribute for methods. This method of using wrappers and \c const is the equivalent to the \c alters_data attribute. You the wrapper writer choose the properties which will be accessible from the templates and makes them \c const, so there's no need to mark other methods as \c alters_data.
For most uses of KTextTemplate, this is enough to get make an application themable easily and quickly. For more advanced usage, see \l {Extending the template system}. For some example uses of KTextTemplate, see \l {Examples of KTextTemplate use}.
\section2 Custom Object Properties
The broad introspection features of Qt5 are relied upon by KTextTemplate via the \c Q_PROPERTY macro. Most types used with that macro may be accessed in %KTextTemplate templates without further code.
We can define a Home type like this:
\code
class Home : public QObject
{
Q_OBJECT
Q_PROPERTY(int houseNumber READ houseNumber)
Q_PROPERTY(QString streetName READ streetName)
Q_PROPERTY(QString city READ city)
public:
// ...
};
\endcode
And we can use it in a \c Q_PROPERTY macro in our \c PersonWrapper type:
\code
class PersonWrapper : public QObject
{
Q_OBJECT
Q_PROPERTY(QString name READ name)
Q_PROPERTY(Home* home READ home)
public:
PersonWrapper(const QString &name, int age, Home *home);
Home* home() const;
// ...
}
\endcode
And then use it in a template:
\code
<h1>{{ person.name }}</h1>
House number: {{ person.home.houseNumber }}
Street: {{ person.home.streetName }}
\endcode
Using containers such as QList and QSharedPointer with the <tt>Q_PROPERTY</tt> macro is also possible.
\code
class PersonWrapper : public QObject
{
Q_OBJECT
Q_PROPERTY(QString name READ name)
Q_PROPERTY(QList<PersonWrapper*> friends READ friends)
public:
PersonWrapper(const QString &name, int age, Home *home);
QList<PersonWrapper> friends() const { return m_friends; }
void setFriends(const QList<PersonWrapper*> friends) { m_friends = friends; }
// ...
private:
QList<PersonWrapper*> m_friends;
};
\endcode
Classes which do not derive from QObject, but which are decorated with the \c Q_GADGET and \c Q_PROPERTY macros may also be used as expected.
\section2 Generic Variables
An alternative to writing wrapper QObject classes with \c Q_PROPERTY macros is to use the KTextTemplate::MetaType system to provide introspection
for any type or any container. This subject is treated in detail in \l {Generic type and template support}.
\section2 Enumerators
KTextTemplate has built-in support for enumerators which use the \c Q_ENUMS macro.
\code
class MyClass : public QObject
{
Q_OBJECT
Q_ENUMS(PersonName)
Q_PROPERTY(PersonName personName READ personName)
public:
enum PersonName
{
Mike,
Natalie,
Oliver,
Patrica = 9
Quentin
};
MyClass(QObject *parent = 0 );
PersonName personName() const { return Quentin; }
};
...
{
QObject *object = new MyClass(this);
context.insert("myObj", object);
t->render(context);
}
\endcode
The enums can be used and accessed in various ways. See QMetaEnum for details.
\code
Oliver is value {{ myObj.Oliver }}.
// Output: "Oliver is value 2".
...
Oliver key is {{ myObj.Oliver.key }}.
// Output: "Oliver key is Oliver".
...
Oliver value is {{ myObj.Oliver.value }}.
// Output: "Oliver value is 2".
...
Oliver scope is {{ myObj.Oliver.scope }}.
// Output: "Oliver scope is MyClass".
...
Oliver scope is {{ myObj.Oliver.name }}.
// Output: "Oliver scope is PersonName".
...
Patrica is value {{ myObj.Patrica }}.
// Output: "Patrica is value 9".
...
PersonName has {{ myObj.PersonName.keyCount }} items.
// Output: "PersonName has 5 items".
...
Second item is {{ myObj.PersonName.1 }}.
// Output: "Second item is 1".
...
Second item is {{ myObj.PersonName.1.key }}.
// Output: "Second item is Natalie".
...
Fourth item is {{ myObj.PersonName.3 }}.
// Output: "Fourth item is 9".
...
Fourth item is {{ myObj.PersonName.3.key }}.
// Output: "Fourth item is Patrica".
...
The personName property is {{ myObj.personName }}.
// Output: "The personName property is 10".
...
The personName property is {{ myObj.personName.key }}.
// Output: "The personName property is Quentin".
...
The personName type is {{ myObj.personName.scope }}.
// Output: "The personName type is PersonName".
...
The personName is {% with myObj.personName as var %}{{ var }}, {{ var.key }}{% endwith %}.
// Output: "The personName is 10, Quentin".
...
The enum is {% with myObj.PersonName as enum %}{{ enum.3 }}, {{ enum.4 }}, {{ enum.4.key }}{% endwith %}.
// Output: "The enum is 9, 10, Quentin".
...
The enum is {% for enum in myObj.PersonName %}{{ enum }} : {{ enum.key }}, {% endfor %}.
// Output: "The enum 0 : Mike, 1 : Natalie, 2 : Oliver, 9 : Patrica, 10 : Quentin, ".
...
The enum is {% for enum in myObj.PersonName %}
{% ifequal enum Oliver %}**{{ enum }} : {{ enum.key }}**
{% else %}{{ enum }} : {{ enum.key }},
{% endifequal %}
{% endfor %}.
// Output: "The enum 0 : Mike, 1 : Natalie, **2 : Oliver**, 9 : Patrica, 10 : Quentin, ".
...
The enum is {% for enum in myObj.PersonName %}
{% ifequal enum myObj.personName %}**{{ enum }} : {{ enum.key }}**
{% else %}{{ enum }} : {{ enum.key }},
{% endifequal %}
{% endfor %}.
// Output: "The enum 0 : Mike, 1 : Natalie, 2 : Oliver, 9 : Patrica, **10 : Quentin**, ".
\endcode
Enumerators in the Qt namespaces are also supported.
\code
AlignRight value is {{ Qt.AlignRight }}.
// Output: "AlignRight value is 2".
...
AlignRight key is {{ Qt.AlignRight.key }}.
// Output: "AlignRight value is AlignRight".
...
AlignRight scope is {{ Qt.AlignRight.scope }}.
// Output: "AlignRight scope is Qt".
...
AlignRight name is {{ Qt.AlignRight.name }}.
// Output: "AlignRight scope is Alignment".
...
{% ifequal myObj.alignment Qt.AlignRight %}RightAligned{% else %}Not RightAligned{% endifequal %}.
// Output: "RightAligned". or "Not RightAligned", depending on the value of myObj.alignment.
...
\endcode
*/
|