Grantlee 5.3.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:
[legend]

## Public Member Functions
| AbstractNodeFactory (QObject *parent={})
| ~AbstractNodeFactory () override
virtual Node * | getNode (const QString &tagContent, Parser *p) const =0

## Protected Member Functions
QList< FilterExpression > | getFilterExpressionList (const QStringList &list, Parser *p) const
Q_INVOKABLE QStringList | smartSplit (const QString &str) const

## 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 );
}
Grantlee::AbstractNodeFactory::smartSplit
Q_INVOKABLE QStringList smartSplit(const QString &str) const
Grantlee::FilterExpression
A FilterExpression object represents a filter expression in a template.
Definition: filterexpression.h:120
Grantlee::Node
Base class for all nodes.
Definition: node.h:83
Grantlee::Parser
The Parser class processes a string template into a tree of nodes.
Definition: parser.h:49

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 );
auto node = new SomeTagNode( arg, p );
auto childNodes = p->parse( node, "end_times" );
node->setChildNodes( childNodes );
p->removeNextToken();
return node;
}
Grantlee::Parser::removeNextToken
void removeNextToken()
Grantlee::Parser::parse
NodeList parse(Node *parent, const QStringList &stopAt={})

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 304 of file node.h.

## Constructor & Destructor Documentation

## ◆ AbstractNodeFactory()

Grantlee::AbstractNodeFactory::AbstractNodeFactory | ( | QObject * | parent = `{}` | ) |
| explicit

Constructor.

Parameters

  parent | The parent QObject

## ◆ ~AbstractNodeFactory()

Grantlee::AbstractNodeFactory::~AbstractNodeFactory | ( |  | ) |
| override

Destructor.

## Member Function Documentation

## ◆ getFilterExpressionList()

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.

## ◆ getNode()

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

## ◆ smartSplit()

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