KTextTemplate::FilterExpression

Search for usage in LXR

KTextTemplate::FilterExpression Class Reference

#include <KTextTemplate/FilterExpression>

Public Member Functions

 FilterExpression ()
 
 FilterExpression (const FilterExpression &other)
 
 FilterExpression (const QString &varString, KTextTemplate::Parser *parser)
 
 ~FilterExpression ()
 
bool isTrue (Context *c) const
 
bool isValid () const
 
FilterExpressionoperator= (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

A FilterExpression object represents a filter expression in a template.

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 );
}
A FilterExpression object represents a filter expression in a template.
Base class for all nodes.
Definition node.h:72
The Parser class processes a string template into a tree of nodes.
Definition parser.h:38
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 );
}
The Context class holds the context to render a Template with.
Definition context.h:107

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 108 of file filterexpression.h.

Constructor & Destructor Documentation

◆ FilterExpression() [1/3]

FilterExpression::FilterExpression ( )

Constructs an invalid FilterExpression.

Definition at line 144 of file filterexpression.cpp.

◆ FilterExpression() [2/3]

FilterExpression::FilterExpression ( const QString & varString,
KTextTemplate::Parser * parser )

Constructs a filter expression from the string varString.

The Parser parser is used to retrieve filters.

Definition at line 72 of file filterexpression.cpp.

◆ FilterExpression() [3/3]

FilterExpression::FilterExpression ( const FilterExpression & other)

Copy constructor.

Definition at line 138 of file filterexpression.cpp.

◆ ~FilterExpression()

FilterExpression::~FilterExpression ( )

Destructor.

Definition at line 155 of file filterexpression.cpp.

Member Function Documentation

◆ isTrue()

bool FilterExpression::isTrue ( Context * c) const

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

See also
Truthiness

Definition at line 236 of file filterexpression.cpp.

◆ isValid()

bool 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.

Definition at line 149 of file filterexpression.cpp.

◆ operator=()

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

Assignment operator.

Definition at line 166 of file filterexpression.cpp.

◆ resolve() [1/2]

QVariant FilterExpression::resolve ( Context * c) const

Resolves the FilterExpression in the Context c.

Definition at line 222 of file filterexpression.cpp.

◆ resolve() [2/2]

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

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

Definition at line 176 of file filterexpression.cpp.

◆ toList()

QVariantList 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.

Definition at line 228 of file filterexpression.cpp.

◆ variable()

Variable FilterExpression::variable ( ) const

Returns the initial variable in the FilterExpression.

Definition at line 160 of file filterexpression.cpp.


The documentation for this class was generated from the following files:
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:17:29 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.