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

Grantlee::Engine is the main entry point for creating Grantlee Templates. More...

#include <grantlee/engine.h>

Inheritance diagram for Grantlee::Engine:
Inheritance graph
[legend]

Public Member Functions

 Engine (QObject *parent=0)
 
 ~Engine ()
 
void addDefaultLibrary (const QString &libName)
 
void addPluginPath (const QString &dir)
 
void addTemplateLoader (AbstractTemplateLoader::Ptr loader)
 
QStringList defaultLibraries () const
 
Template loadByName (const QString &name) const
 
QPair< QString, QStringmediaUri (const QString &fileName) const
 
Template newTemplate (const QString &content, const QString &name) const
 
QStringList pluginPaths () const
 
void removeDefaultLibrary (const QString &libName)
 
void removePluginPath (const QString &dir)
 
void setPluginPaths (const QStringList &dirs)
 
void setSmartTrimEnabled (bool enabled)
 
bool smartTrimEnabled () const
 
QList
< AbstractTemplateLoader::Ptr
templateLoaders ()
 
- Public Member Functions inherited from QObject
Q_INVOKABLE QObject (QObject *parent)
 
 QObject (QObject *parent, const char *name)
 
bool blockSignals (bool block)
 
QObjectchild (const char *objName, const char *inheritsClass, bool recursiveSearch) const
 
const QObjectList & children () const
 
const char * className () const
 
bool connect (const QObject *sender, const char *signal, const char *method, Qt::ConnectionType type) const
 
void deleteLater ()
 
void destroyed (QObject *obj)
 
bool disconnect (const QObject *receiver, const char *method)
 
bool disconnect (const char *signal, const QObject *receiver, const char *method)
 
void dumpObjectInfo ()
 
void dumpObjectTree ()
 
QList< QByteArraydynamicPropertyNames () const
 
virtual bool event (QEvent *e)
 
virtual bool eventFilter (QObject *watched, QEvent *event)
 
findChild (const QString &name) const
 
QList< T > findChildren (const QRegExp &regExp) const
 
QList< T > findChildren (const QString &name) const
 
bool inherits (const char *className) const
 
void insertChild (QObject *object)
 
void installEventFilter (QObject *filterObj)
 
bool isA (const char *className) const
 
bool isWidgetType () const
 
void killTimer (int id)
 
virtual const QMetaObjectmetaObject () const
 
void moveToThread (QThread *targetThread)
 
const char * name () const
 
const char * name (const char *defaultName) const
 
QString objectName () const
 
QObjectparent () const
 
QVariant property (const char *name) const
 
void removeChild (QObject *object)
 
void removeEventFilter (QObject *obj)
 
void setName (const char *name)
 
void setObjectName (const QString &name)
 
void setParent (QObject *parent)
 
bool setProperty (const char *name, const QVariant &value)
 
bool signalsBlocked () const
 
int startTimer (int interval)
 
QThreadthread () const
 

Additional Inherited Members

- Static Public Member Functions inherited from QObject
bool connect (const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
 
bool disconnect (const QObject *sender, const char *signal, const QObject *receiver, const char *method)
 
QString tr (const char *sourceText, const char *disambiguation, int n)
 
QString trUtf8 (const char *sourceText, const char *disambiguation, int n)
 
- Protected Member Functions inherited from QObject
bool checkConnectArgs (const char *signal, const QObject *object, const char *method)
 
virtual void childEvent (QChildEvent *event)
 
virtual void connectNotify (const char *signal)
 
virtual void customEvent (QEvent *event)
 
virtual void disconnectNotify (const char *signal)
 
int receivers (const char *signal) const
 
QObjectsender () const
 
virtual void timerEvent (QTimerEvent *event)
 
- Static Protected Member Functions inherited from QObject
QByteArray normalizeSignalSlot (const char *signalSlot)
 
- Properties inherited from QObject
 objectName
 

Detailed Description

The Grantlee::Engine is responsible for configuring and creating Template objects. In typical use, one or more TemplateLoader objects will be added to the Engine to load template objects, and plugin directories will be set to enable finding template tags and filters.

Engine *engine = new Engine();
FileSystemTemplateLoader::Ptr loader = FileSystemTemplateLoader::Ptr( new FileSystemTemplateLoader() );
loader->setTemplateDirs( QStringList() << "/usr/share/myapp/templates" );
engine->addTemplateLoader( loader );
engine->addPluginPath( "/usr/lib/myapp" );
Template template1 = engine->newTemplate( "Template content", "template name" );
Template template2 = engine->loadByName( "templatefile.html" );

Once it is configured, the engine can be used to create new templates by name by loading the templates with the loadByName method, or by defining the content in the newTemplate method.

By default the builtin tags and filters distributed with Grantlee are available in all templates without using the {% load %} tag in the template. These pre-loaded libraries may be configured if appropriate to the application. For example, an application which defines its own tags and filters may want them to be always available, or it may be desirable to restrict the features available to template authors by removing built in libraries.

Different Engine objects can be used to create templates with differing configurations.

Insignificant whitespace

The output of rendering a template depends on the content of the template. In some cases when generating content in which whitespace is significant, this can have undesired effects. For example, given a template to generate C++ code like:

  class MyClass {
  {# This loop creates the  #}
  {# methods in the class   #}
  {% for method in methods %}
    {% if method.hasDox %}
      {{ method.dox }}
    {% endif %}
      {{ method.signature }}
  {% endfor %}
  };

The output would have a lot of whitespace which is not necessarily wanted.

class MyClass {
void foo() const;
};

It is possible to strip insignificant whitespace by enabling the smartTrim feature with setSmartTrimEnabled. When enabled the output will not contain a newline for any line in the template which has only one token of template syntax, such as a comment, tag or variable.

class MyClass {
void foo() const;
};
Author
Stephen Kelly steve.nosp@m.ire@.nosp@m.gmail.nosp@m..com

Definition at line 110 of file engine.h.

Constructor & Destructor Documentation

Grantlee::Engine::Engine ( QObject parent = 0)

Constructor

Grantlee::Engine::~Engine ( )

Destructor.

Member Function Documentation

void Grantlee::Engine::addDefaultLibrary ( const QString libName)

Adds the library named libName to the libraries available by default to new Templates.

void Grantlee::Engine::addPluginPath ( const QString dir)

Prepend path to the list of plugin dirs.

void Grantlee::Engine::addTemplateLoader ( AbstractTemplateLoader::Ptr  loader)

Adds loader to the TemplateLoaders currently configured on the Engine.

QStringList Grantlee::Engine::defaultLibraries ( ) const

Returns the libraries available by default to new Templates.

Template Grantlee::Engine::loadByName ( const QString name) const

Load the Template identified by name.

The Templates and plugins loaded will be determined by the Engine configuration.

QPair<QString, QString> Grantlee::Engine::mediaUri ( const QString fileName) const

Returns a URI for a media item with the name name.

Typically this will be used for images. For example the media URI for the image "header_logo.png" may be "/home/user/common/header_logo.png" or "/home/user/some_theme/header_logo.png" depending on the templateLoaders configured.

This method will not usually be called by application code. To load media in a template, use the {% media_finder %} template tag.

Template Grantlee::Engine::newTemplate ( const QString content,
const QString name 
) const

Create a new Template with the content content identified by name.

The secondary Templates and plugins loaded will be determined by the Engine configuration.

QStringList Grantlee::Engine::pluginPaths ( ) const

Returns the currently configured plugin dirs

void Grantlee::Engine::removeDefaultLibrary ( const QString libName)

Removes the library named libName from the libraries available by default to new Templates.

void Grantlee::Engine::removePluginPath ( const QString dir)

Removes all instances of dir from the list of plugin dirs.

void Grantlee::Engine::setPluginPaths ( const QStringList dirs)

Sets the plugin dirs currently configured on the Engine to dirs.

Warning
This overwrites the default paths. You normally want addPluginPath.
See Also
Finding tags and filters
void Grantlee::Engine::setSmartTrimEnabled ( bool  enabled)

Sets whether the smart trim feature is enabled for newly loaded templates.

See Also
smart_trim
bool Grantlee::Engine::smartTrimEnabled ( ) const

Returns whether the smart trim feature is enabled for newly loaded templates.

See Also
smart_trim

This is false by default.

QList<AbstractTemplateLoader::Ptr> Grantlee::Engine::templateLoaders ( )

Returns the TemplateLoaders currently configured on the Engine.