KTextTemplate::FileSystemTemplateLoader Class

class KTextTemplate::FileSystemTemplateLoader

The FileSystemTemplateLoader loads Templates from the file system. More...

Header: #include <KTextTemplate/TemplateLoader>
CMake: find_package(KF6 REQUIRED COMPONENTS TextTemplate)
target_link_libraries(mytarget PRIVATE KF6::TextTemplate)
Inherits: KTextTemplate::AbstractTemplateLoader

Public Functions

FileSystemTemplateLoader(const QSharedPointer<KTextTemplate::AbstractLocalizer> localizer = {})
void setTemplateDirs(const QStringList &dirs)
void setTheme(const QString &themeName)
QStringList templateDirs() const
QString themeName() const

Reimplemented Public Functions

virtual bool canLoadTemplate(const QString &name) const override
virtual std::pair<QString, QString> getMediaUri(const QString &fileName) const override
virtual KTextTemplate::Template loadByName(const QString &name, const KTextTemplate::Engine *engine) const override

Detailed Description

This template loader works by traversing a list of directories to find templates. Directories are checked in order, and the first match hit is parsed and returned.

loader->setTemplateDirs({
    "/home/user/app/templates",
    "/usr/local/share/app/templates"
});
engine->setTemplateLoader( loader );

// This will try /home/user/app/templates/mytemplate.html
// followed by /usr/local/share/app/templates/mytemplate.html
engine->loadByName( "mytemplate.html" );

Additionally, a themeName may be set on the template loader, which will be appended to search paths before the template name.

loader->setTemplateDirs({
  "/home/user/app/templates" <<
  "/usr/local/share/app/templates"
});
loader->setTheme( "simple_theme" );
engine->setTemplateLoader( loader );

// This will try /home/user/app/templates/simple_theme/mytemplate.html
// followed by /usr/local/share/app/templates/simple_theme/mytemplate.html
engine->loadByName( "mytemplate.html" );

Media URIs may be retrieved for media relative to the directories searched queried for templates.

loader->setTemplateDirs({
  "/home/user/app/templates",
  "/usr/local/share/app/templates"
});
loader->setTheme( "simple_theme" );
engine->setTemplateLoader( loader );

// This will try /home/user/app/templates/simple_theme/logo.png
// followed by /usr/local/share/app/templates/simple_theme/logo.png
// and return the first one that exists.
engine->mediaUri( "logo.png" );

The template files loaded by a FileSystemTemplateLoader must be UTF-8 encoded.

Member Function Documentation

FileSystemTemplateLoader::FileSystemTemplateLoader(const QSharedPointer<KTextTemplate::AbstractLocalizer> localizer = {})

Constructor

[override virtual] bool FileSystemTemplateLoader::canLoadTemplate(const QString &name) const

Reimplements: AbstractTemplateLoader::canLoadTemplate(const QString &name) const.

[override virtual] std::pair<QString, QString> FileSystemTemplateLoader::getMediaUri(const QString &fileName) const

Reimplements: AbstractTemplateLoader::getMediaUri(const QString &fileName) const.

[override virtual] KTextTemplate::Template FileSystemTemplateLoader::loadByName(const QString &name, const KTextTemplate::Engine *engine) const

Reimplements: AbstractTemplateLoader::loadByName(const QString &name, const KTextTemplate::Engine *engine) const.

void FileSystemTemplateLoader::setTemplateDirs(const QStringList &dirs)

Sets the directories to look for template files to dirs.

See also templateDirs().

void FileSystemTemplateLoader::setTheme(const QString &themeName)

Sets the theme of this loader to themeName

QStringList FileSystemTemplateLoader::templateDirs() const

The directories this TemplateLoader looks in for template files.

See also setTemplateDirs().

QString FileSystemTemplateLoader::themeName() const

The themeName of this TemplateLoader