Grantlee 5.3.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 FilterExpression &other)
| FilterExpression (const QString &varString, Grantlee::Parser *parser)
| ~FilterExpression ()
bool | isTrue (Context *c) const
bool | isValid () const
FilterExpression & | operator= (const FilterExpression &other)
QVariant | resolve (Context *c) const
QVariant | resolve (OutputStream *stream, 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 {
auto 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::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

See also
  AbstractNodeFactory::getFilterExpressionList

When implementing the Node::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 );
}
Grantlee::Context
The Context class holds the context to render a Template with.
Definition: context.h:118

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 );
else
m_falseList.render( stream, c );
}

See also
  Tags with end tags

Author
  Stephen Kelly steve.nosp@m.ire@.nosp@m.gmail.nosp@m..com

Definition at line 119 of file filterexpression.h.

## Constructor & Destructor Documentation

## ◆ FilterExpression() [1/3]

Grantlee::FilterExpression::FilterExpression | ( |  | ) |

Constructs an invalid FilterExpression.

## ◆ FilterExpression() [2/3]

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.

## ◆ FilterExpression() [3/3]

Grantlee::FilterExpression::FilterExpression | ( | const FilterExpression & | other | ) |

Copy constructor.

## ◆ ~FilterExpression()

Grantlee::FilterExpression::~FilterExpression | ( |  | ) |

Destructor.

## Member Function Documentation

## ◆ isTrue()

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

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

See also
  Truthiness

## ◆ isValid()

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.

## ◆ operator=()

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

Assignment operator.

## ◆ resolve() [1/2]

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

Resolves the FilterExpression in the Context `c`.

## ◆ resolve() [2/2]

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

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

## ◆ toList()

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()

Variable Grantlee::FilterExpression::variable | ( |  | ) | const

Returns the initial variable in the FilterExpression.