KTextTemplate

templateloader.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_TEMPLATELOADER_H
11#define KTEXTTEMPLATE_TEMPLATELOADER_H
12
13#include "ktexttemplate_export.h"
14#include "template.h"
15
16#include <QSharedPointer>
17
18namespace KTextTemplate
19{
20
21class AbstractLocalizer;
22
23/// @headerfile templateloader.h <KTextTemplate/TemplateLoader>
24
25/**
26 @brief An retrieval interface to a storage location for Template objects.
27
28 This interface can be implemented to define new ways of retrieving the content
29 of Templates.
30
31 The interface of this class should not be called directly from applications.
32 TemplateLoaders will typically be created, configured and added to the
33 KTextTemplate::Engine which will call the appropriate API.
34
35 @author Stephen Kelly <steveire@gmail.com>
36*/
37class KTEXTTEMPLATE_EXPORT AbstractTemplateLoader
38{
39public:
40 /**
41 Destructor
42 */
44
45 /**
46 Load a Template called @p name. Return an invalid Template if no content
47 by that name exists.
48 */
49 virtual Template loadByName(const QString &name, Engine const *engine) const = 0;
50
51 /**
52 Return a complete URI for media identified by fileName.
53 */
54 virtual std::pair<QString, QString> getMediaUri(const QString &fileName) const = 0;
55
56 /**
57 Return true if a Template identified by @p name exists and can be loaded.
58 */
59 virtual bool canLoadTemplate(const QString &name) const = 0;
60};
61
62/// @headerfile templateloader.h KTextTemplate/templateloader.h
63
64class FileSystemTemplateLoaderPrivate;
65
66/**
67 @brief The **%FileSystemTemplateLoader** loads Templates from the file system.
68
69 This template loader works by traversing a list of directories to find
70 templates. Directories are checked in order, and the first match hit is parsed
71 and returned.
72
73 @code
74 loader->setTemplateDirs({
75 "/home/user/app/templates",
76 "/usr/local/share/app/templates"
77 });
78 engine->setTemplateLoader( loader );
79
80 // This will try /home/user/app/templates/mytemplate.html
81 // followed by /usr/local/share/app/templates/mytemplate.html
82 engine->loadByName( "mytemplate.html" );
83 @endcode
84
85 Additionally, a themeName may be set on the template loader, which will be
86 appended to search paths before the template name.
87
88 @code
89 loader->setTemplateDirs({
90 "/home/user/app/templates" <<
91 "/usr/local/share/app/templates"
92 });
93 loader->setTheme( "simple_theme" );
94 engine->setTemplateLoader( loader );
95
96 // This will try /home/user/app/templates/simple_theme/mytemplate.html
97 // followed by /usr/local/share/app/templates/simple_theme/mytemplate.html
98 engine->loadByName( "mytemplate.html" );
99 @endcode
100
101 Media URIs may be retrieved for media relative to the directories searched
102 queried for templates.
103
104 @code
105 loader->setTemplateDirs({
106 "/home/user/app/templates",
107 "/usr/local/share/app/templates"
108 });
109 loader->setTheme( "simple_theme" );
110 engine->setTemplateLoader( loader );
111
112 // This will try /home/user/app/templates/simple_theme/logo.png
113 // followed by /usr/local/share/app/templates/simple_theme/logo.png
114 // and return the first one that exists.
115 engine->mediaUri( "logo.png" );
116 @endcode
117
118 The template files loaded by a %**FileSystemTemplateLoader** must be UTF-8
119 encoded.
120
121 @see @ref deploying_templates
122
123*/
124class KTEXTTEMPLATE_EXPORT FileSystemTemplateLoader : public AbstractTemplateLoader
125{
126public:
127 /**
128 Constructor
129 */
131
132 /**
133 Destructor
134 */
135 ~FileSystemTemplateLoader() override;
136
137 Template loadByName(const QString &name, Engine const *engine) const override;
138
139 bool canLoadTemplate(const QString &name) const override;
140
141 std::pair<QString, QString> getMediaUri(const QString &fileName) const override;
142
143 /**
144 Sets the theme of this loader to @p themeName
145 */
146 void setTheme(const QString &themeName);
147
148 /**
149 The themeName of this TemplateLoader
150 */
151 QString themeName() const;
152
153 /**
154 Sets the directories to look for template files to @p dirs.
155 */
156 void setTemplateDirs(const QStringList &dirs);
157
158 /**
159 The directories this TemplateLoader looks in for template files.
160 */
161 QStringList templateDirs() const;
162
163private:
164 Q_DECLARE_PRIVATE(FileSystemTemplateLoader)
165 FileSystemTemplateLoaderPrivate *const d_ptr;
166};
167
168/// @headerfile templateloader.h KTextTemplate/templateloader.h
169
170/**
171 @brief The **%InMemoryTemplateLoader** loads Templates set dynamically in
172 memory
173
174 This class is mostly used for testing purposes, but can also be used for
175 simple uses of %KTextTemplate.
176
177 Templates can be made available using the @ref setTemplate method, and will
178 then be retrieved by the KTextTemplate::Engine as appropriate.
179*/
180class KTEXTTEMPLATE_EXPORT InMemoryTemplateLoader : public AbstractTemplateLoader
181{
182public:
184 ~InMemoryTemplateLoader() override;
185
186 Template loadByName(const QString &name, Engine const *engine) const override;
187
188 bool canLoadTemplate(const QString &name) const override;
189
190 std::pair<QString, QString> getMediaUri(const QString &fileName) const override;
191
192 /**
193 Add a template content to this Loader.
194
195 Example:
196
197 @code
198 auto loader = QSharedPointer<InMemoryTemplateLoader::create();
199 loader->setTemplate( "name_template", "My name is {{ name }}" );
200 loader->setTemplate( "age_template", "My age is {{ age }}" );
201 engine->addTemplateLoader( loader );
202
203 // Both templates may now be retrieved by calling Engine::loadByName.
204 @endcode
205 */
206 void setTemplate(const QString &name, const QString &content);
207
208private:
209 QHash<QString, QString> m_namedTemplates;
210};
211}
212
213#endif
An retrieval interface to a storage location for Template objects.
virtual ~AbstractTemplateLoader()
Destructor.
virtual bool canLoadTemplate(const QString &name) const =0
Return true if a Template identified by name exists and can be loaded.
virtual std::pair< QString, QString > getMediaUri(const QString &fileName) const =0
Return a complete URI for media identified by fileName.
virtual Template loadByName(const QString &name, Engine const *engine) const =0
Load a Template called name.
KTextTemplate::Engine is the main entry point for creating KTextTemplate Templates.
Definition engine.h:110
The FileSystemTemplateLoader loads Templates from the file system.
The InMemoryTemplateLoader loads Templates set dynamically in memory.
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
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:17:29 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.