KTextTemplate

template.h
1/*
2 This file is part of the KTextTemplate library
3
4 SPDX-FileCopyrightText: 2009, 2010 Stephen Kelly <steveire@gmail.com>
5
6 SPDX-License-Identifier: LGPL-2.1-or-later
7
8*/
9
10#ifndef KTEXTTEMPLATE_TEMPLATE_H
11#define KTEXTTEMPLATE_TEMPLATE_H
12
13#include "exception.h"
14#include "ktexttemplate_export.h"
15#include "node.h"
16
17#include <QSharedPointer>
18#include <QStringList>
19
20namespace KTextTemplate
21{
22class Context;
23class Engine;
24class TemplateImpl;
25class OutputStream;
26
27#ifdef K_DOXYGEN
28#define TemplateImpl Template
29#else
30typedef QSharedPointer<TemplateImpl> Template;
31#endif
32
33class TemplatePrivate;
34
35/// @headerfile template.h <KTextTemplate/Template>
36
37/**
38 @brief The **%Template** class is a tree of nodes which may be rendered.
39
40 All Templates are created through the KTextTemplate::Engine class.
41 A **%Template** is created by parsing some text markup passed into the Engine,
42 or by reading it from a file.
43
44 Note that **%Template** is actually a typedef for a
45 <tt>QSharedPointer&lt;TemplateImpl&gt;</tt>, so all of its members should be
46 accessed with <tt>operator-&gt;()</tt>.
47
48 The result of parsing is a **%Template** object which can be rendered multiple
49 times with multiple different Contexts.
50
51 @code
52 auto engine = getEngine();
53 auto t = engine->newTemplate(
54 "{{ name }} is aged {{ age }}",
55 "simple template" );
56 if ( t->error() )
57 {
58 // Tokenizing or parsing error, or couldn't find custom tags or filters.
59 qDebug() << t->errorString();
60 return;
61 }
62 QTextStream textStream( stdout );
63 OutputStream stream( textStream );
64
65 for ( ... )
66 {
67 Context c;
68 // ... c.insert
69 t->render( stream, c );
70
71 if (t->error())
72 {
73 // Rendering error.
74 qDebug() << t->errorString();
75 }
76 }
77 @endcode
78
79 If there is an error in parsing or rendering, the @ref error and @ref
80 errorString methods can be used to check the source of the error.
81
82 @author Stephen Kelly <steveire@gmail.com>
83*/
84class KTEXTTEMPLATE_EXPORT TemplateImpl : public QObject
85{
86 Q_OBJECT
87public:
88 ~TemplateImpl() override;
89
90 /**
91 Renders the **%Template** to a string given the Context @p c.
92 */
93 QString render(Context *c) const;
94
95 /**
96 Renders the **%Template** to the OutputStream @p stream given the Context c.
97 */
98 OutputStream *render(OutputStream *stream, Context *c) const;
99
100#ifndef K_DOXYGEN
101 /**
102 @internal
103 */
104 NodeList nodeList() const;
105
106 /**
107 @internal
108 */
109 void setNodeList(const NodeList &list);
110#endif
111
112 /**
113 Returns an error code for the error encountered.
114 */
115 Error error() const;
116
117 /**
118 Returns more information to developers in the form of a string.
119 */
120 QString errorString() const;
121
122 /**
123 Returns the Engine that created this **%Template**.
124 */
125 Engine const *engine() const;
126
127#ifndef K_DOXYGEN
128protected:
129 TemplateImpl(Engine const *engine, QObject *parent = {});
130 TemplateImpl(Engine const *engine, bool smartTrim, QObject *parent = {});
131
132 void setContent(const QString &templateString);
133#endif
134
135private:
136 // Don't allow setting the parent on a Template, which is memory managed as
137 // a QSharedPointer.
138 using QObject::setParent;
139
140private:
141 Q_DECLARE_PRIVATE(Template)
142 TemplatePrivate *const d_ptr;
143#ifndef K_DOXYGEN
144 friend class Engine;
145 friend class Parser;
146#endif
147};
148}
149
150#endif
The Context class holds the context to render a Template with.
Definition context.h:107
KTextTemplate::Engine is the main entry point for creating KTextTemplate Templates.
Definition engine.h:110
A list of Nodes with some convenience API for rendering them.
Definition node.h:141
The OutputStream class is used to render templates to a QTextStream.
The Parser class processes a string template into a tree of nodes.
Definition parser.h:38
The Template class is a tree of nodes which may be rendered.
Definition template.h:85
The KTextTemplate namespace holds all public KTextTemplate API.
Definition Mainpage.dox:8
Error
Types of errors that can occur while using KTextTemplate.
Definition exception.h:25
void setParent(QObject *parent)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Jul 26 2024 12:01:00 by doxygen 1.11.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.