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

A FilterExpression object represents a filter expression in a template. More...

#include <grantlee/filterexpression.h>

Public Member Functions

 FilterExpression ()
 FilterExpression (const QString &varString, Grantlee::Parser *parser)
 FilterExpression (const FilterExpression &other)
 ~FilterExpression ()
bool isTrue (Context *c) const
bool isValid () const
FilterExpressionoperator= (const FilterExpression &other)
QVariant resolve (OutputStream *stream, Context *c) const
QVariant resolve (Context *c) const
QVariantList toList (Context *c) const
Variable variable () const

Detailed Description

This class is only relevant if implementing custom tags or filters. Most of the API here is internal. Usually when implementing tags or filters, filter expressions will just be created and resolved.

In template markup, a filter expression is a variable followed by one or more filters separated by pipes:

Filter expressions may appear in variable nodes:

{{ some_var|upper_filter|lower_filter }}

Or as arguments to tags

{% some_tag some_arg1|filter1|filter2 some_arg2|filter3 %}

The FilterExpression class would be used in the getNode implementation of the AbstractNodeFactory implementation for the some_tag tag.

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

When implementing the AbstractNodeFactory::render method, the resolve method may be used to process the filter expression.

For example, if our SomeTagNode was to concatenate the resolved values given as arguments:

void SomeTagNode::render( QTextStream *stream, Context *c ) {
m_arg1.resolve( stream, c );
m_arg2.resolve( stream, c );

Because Filters are highly generic, they do not all write data to the stream. For example, a Filter might take as input a string, and return a list by splitting the string on commas, or a Filter might compare an input to its argument and return whether they are the same, but not write anything to the stream. For that reason, the resolve method writes data to the given stream, and returns the same data in its returned QVariant.

The suitability of either of the resolve methods will depend on the implementation and requirements of your custom tag. For example if the SomeTagNode ran a comparison of the arguments:

void SomeTagNode::render( QTextStream *stream, Context *c ) {
QString first = m_arg1.resolve( c ).toString();
QString second = m_arg2.resolve( c ).toString();
if ( first == second )
m_trueList.render( stream, c );
m_falseList.render( stream, c );
See also
Tags with end tags
Stephen Kelly

Definition at line 118 of file filterexpression.h.

Constructor & Destructor Documentation

Grantlee::FilterExpression::FilterExpression ( )

Constructs an invalid FilterExpression.

Grantlee::FilterExpression::FilterExpression ( const QString varString,
Grantlee::Parser parser 

Constructs a filter expression from the string varString. The Parser parser is used to retrieve filters.

Grantlee::FilterExpression::FilterExpression ( const FilterExpression other)

Copy constructor.

Grantlee::FilterExpression::~FilterExpression ( )


Member Function Documentation

bool Grantlee::FilterExpression::isTrue ( Context c) const

Returns whether the Filter resolves to true in the Context c.

See also
bool Grantlee::FilterExpression::isValid ( ) const

Returns whether a filter expression is valid.

A FilterExpression is valid if all filters in the expression exist and the initial variable being filtered is valid.

FilterExpression& Grantlee::FilterExpression::operator= ( const FilterExpression other)

Assignment operator.

QVariant Grantlee::FilterExpression::resolve ( OutputStream stream,
Context c 
) const

Resolves the FilterExpression in the Context c and writes it to the stream stream.

QVariant Grantlee::FilterExpression::resolve ( Context c) const

Resolves the FilterExpression in the Context c.

QVariantList Grantlee::FilterExpression::toList ( Context c) const

Returns a list for the FilterExpression. If the FilterExpression can not be resolved to a list, an empty list will be returned.

Variable Grantlee::FilterExpression::variable ( ) const

Returns the initial variable in the FilterExpression.