Grantlee: Grantlee::FileSystemTemplateLoader Class Reference

Grantlee 5.1.0

Public Member Functions | List of all members
Grantlee::FileSystemTemplateLoader Class Reference

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

`#include <templateloader.h>`

Inheritance diagram for Grantlee::FileSystemTemplateLoader:
[legend]

## Public Member Functions
| FileSystemTemplateLoader (const QSharedPointer< AbstractLocalizer > localizer=QSharedPointer< AbstractLocalizer >())
| ~FileSystemTemplateLoader () override
bool | canLoadTemplate (const QString &name) const override
QPair< QString, QString > | getMediaUri (const QString &fileName) const override
Template | loadByName (const QString &name, Engine const *engine) const override
void | setTemplateDirs (const QStringList &dirs)
void | setTheme (const QString &themeName)
QStringList | templateDirs () const
QString | themeName () const
Public Member Functions inherited from Grantlee::AbstractTemplateLoader
virtual | ~AbstractTemplateLoader ()

## 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(QStringList() << "/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(QStringList() << "/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(QStringList() << "/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.

See also
  Deploying Templates

Definition at line 133 of file templateloader.h.

## Constructor & Destructor Documentation

## ◆ FileSystemTemplateLoader()

Grantlee::FileSystemTemplateLoader::FileSystemTemplateLoader | ( | const QSharedPointer< AbstractLocalizer > | localizer = `QSharedPointer< AbstractLocalizer >()` | ) |

Constructor

## ◆ ~FileSystemTemplateLoader()

Grantlee::FileSystemTemplateLoader::~FileSystemTemplateLoader | ( |  | ) |
| override

Destructor

## Member Function Documentation

## ◆ canLoadTemplate()

bool Grantlee::FileSystemTemplateLoader::canLoadTemplate | ( | const QString & | name | ) | const
| overridevirtual

Return true if a Template identified by `name` exists and can be loaded.

Implements Grantlee::AbstractTemplateLoader.

## ◆ getMediaUri()

QPair<QString, QString> Grantlee::FileSystemTemplateLoader::getMediaUri | ( | const QString & | fileName | ) | const
| overridevirtual

Return a complete URI for media identified by fileName.

Implements Grantlee::AbstractTemplateLoader.

## ◆ loadByName()

Template Grantlee::FileSystemTemplateLoader::loadByName | ( | const QString & | name,
|  | Engine const * | engine
| ) |  | const
| overridevirtual

Load a Template called `name`. Return an invalid Template if no content by that name exists.

Implements Grantlee::AbstractTemplateLoader.

## ◆ setTemplateDirs()

void Grantlee::FileSystemTemplateLoader::setTemplateDirs | ( | const QStringList & | dirs | ) |

Sets the directories to look for template files to `dirs`.

## ◆ setTheme()

void Grantlee::FileSystemTemplateLoader::setTheme | ( | const QString & | themeName | ) |

Sets the theme of this loader to `themeName`

## ◆ templateDirs()

QStringList Grantlee::FileSystemTemplateLoader::templateDirs | ( |  | ) | const

The directories this TemplateLoader looks in for template files.

## ◆ themeName()

QString Grantlee::FileSystemTemplateLoader::themeName | ( |  | ) | const

The themeName of this TemplateLoader