Grantlee  0.2.0
Public Member Functions | Protected Member Functions
Grantlee::AbstractNodeFactory Class Reference

Base class for all NodeFactories. More...

#include <grantlee/node.h>

List of all members.

Public Member Functions

 AbstractNodeFactory (QObject *parent=0)
virtual ~AbstractNodeFactory ()
virtual NodegetNode (const QString &tagContent, Parser *p) const =0

Protected Member Functions

QList< FilterExpressiongetFilterExpressionList (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 );
    }

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


Constructor & Destructor Documentation

Grantlee::AbstractNodeFactory::AbstractNodeFactory ( QObject *  parent = 0) [explicit]

Constructor.

Parameters:
parentThe parent QObject

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