Grantlee  5.1.0
Public Member Functions | Protected Member Functions | List of all members
Grantlee::AbstractNodeFactory Class Referenceabstract

Base class for all NodeFactories. More...

#include <grantlee/node.h>

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

Public Member Functions

 AbstractNodeFactory (QObject *parent=0)
 
 ~AbstractNodeFactory () override
 
virtual NodegetNode (const QString &tagContent, Parser *p) const =0
 
- 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
 

Protected Member Functions

QList< FilterExpressiongetFilterExpressionList (const QStringList &list, Parser *p) const
 
Q_INVOKABLE QStringList smartSplit (const QString &str) const
 
- 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)
 

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)
 
- Static Protected Member Functions inherited from QObject
QByteArray normalizeSignalSlot (const char *signalSlot)
 
- Properties inherited from QObject
 objectName
 

Detailed Description

This class can be used to make custom tags available to templates. The getNode method should be implemented to return a Node to be rendered.

A node is represented in template markup as content surrounded by percent signed tokens.

text content
{% some_tag arg1 arg2 %}
text content
{% some_other_tag arg1 arg2 %}
text content
{% end_some_other_tag %}
text content

It is the responsibility of an AbstractNodeFactory implementation to process the contents of a tag and return a Node implementation from its getNode method.

The getNode method would for example be called with the tagContent "some_tag arg1 arg2". That content could then be split up, the arguments processed and a Node created

Node* SomeTagFactory::getNode(const QString &tagContent, Parser *p) {
QStringList parts = smartSplit( tagContent );
parts.removeFirst(); // Remove the "some_tag" part.
FilterExpression arg1( parts.first(), p );
FilterExpression arg2( parts.at( 1 ), p );
return new SomeTagNode( arg1, arg2, p );
}

The getNode implementation might also advance the parser. For example if we had a {% times %} tag which rendered content the amount of times it was given in its argument, it could be used like this:

Some text content.
{% times 5 %}
the bit to be repeated
{% end_times %}
End text content

The argument to {% times %} might not be a simple number, but could be a FilterExpression such as someobject.some_property|getDigit:1.

The implementation could look like

Node* SomeOtherTagFactory::getNode(const QString &tagContent, Parser *p) {
QStringList parts = smartSplit( tagContent );
parts.removeFirst(); // Remove the "times" part.
FilterExpression arg( parts.first(), p );
SomeTagNode *node = new SomeTagNode( arg, p );
NodeList childNodes = p->parse( node, "end_times" );
node->setChildNodes( childNodes );
p->removeNextToken();
return node;
}

Note that it is necessary to invoke the parser to create the child nodes only after creating the Node to return. That node must be passed to the Parser to perform as the parent QObject to the child nodes.

See also
Parser::parse

Definition at line 306 of file node.h.

Constructor & Destructor Documentation

Grantlee::AbstractNodeFactory::AbstractNodeFactory ( QObject parent = 0)
explicit

Constructor.

Parameters
parentThe parent QObject
Grantlee::AbstractNodeFactory::~AbstractNodeFactory ( )
override

Destructor.

Member Function Documentation

QList<FilterExpression> Grantlee::AbstractNodeFactory::getFilterExpressionList ( const QStringList list,
Parser p 
) const
protected

Returns a list of FilterExpression objects created with Parser p as described by the content of list.

This is used for convenience when handling the arguments to a tag.

virtual Node* Grantlee::AbstractNodeFactory::getNode ( const QString tagContent,
Parser p 
) const
pure virtual

This method should be reimplemented to return a Node which can be rendered.

The tagContent is the content of the tag including the tag name and arguments. For example, if the template content is {% my_tag arg1 arg2 %}, the tagContent will be "my_tag arg1 arg2".

The Parser p is available and can be advanced if appropriate. For example, if the tag has an end tag, the parser can be advanced to the end tag.

See also
Tags
Q_INVOKABLE QStringList Grantlee::AbstractNodeFactory::smartSplit ( const QString str) const
protected

Splits str into a list, taking quote marks into account.

This is typically used in the implementation of getNode with the tagContent.

If str is 'one "two three" four 'five " six' seven', the returned list will contain the following strings:

  • one
  • "two three"
  • four
  • five " six
  • seven