2
This file is part of the Grantlee template system.
4
Copyright (c) 2009,2010 Stephen Kelly <steveire@gmail.com>
6
This library is free software; you can redistribute it and/or
7
modify it under the terms of the GNU Lesser General Public
8
License as published by the Free Software Foundation; either version
9
2 of the Licence, or (at your option) any later version.
11
This library is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
Library General Public License for more details.
16
You should have received a copy of the GNU Lesser General Public
17
License along with this library. If not, see <http://www.gnu.org/licenses/>.
23
#include <QtCore/QStringList>
26
#include "exception.h"
29
IfNodeFactory::IfNodeFactory()
35
Node* IfNodeFactory::getNode( const QString &tagContent, Parser *p ) const
37
QStringList expr = smartSplit( tagContent );
39
if ( expr.size() <= 0 ) {
40
throw Grantlee::Exception( TagSyntaxError, "'if' statement requires at least one argument" );
43
int linkType = IfNode::OrLink;
45
QString exprString = expr.join( QChar( ' ' ) );
47
QStringList boolPairs = exprString.split( " and " );
49
if ( boolPairs.size() == 1 ) {
50
boolPairs = exprString.split( " or " );
52
linkType = IfNode::AndLink;
53
if ( exprString.contains( " or " ) ) {
54
throw Grantlee::Exception( TagSyntaxError, "'if' tags can't mix 'and' and 'or'" );
58
QList<QPair<bool, FilterExpression > > boolVars;
59
Q_FOREACH( const QString &boolStr, boolPairs ) {
60
QPair<bool, FilterExpression> pair;
61
if ( boolStr.contains( ' ' ) ) {
62
QStringList bits = boolStr.split( ' ' );
63
if ( bits.size() != 2 ) {
64
throw Grantlee::Exception( TagSyntaxError, "'if' statement improperly formatted" );
66
if ( bits.at( 0 ) != "not" ) {
67
throw Grantlee::Exception( TagSyntaxError, "Expected 'not' in if statement" );
70
pair.second = FilterExpression( bits.at( 1 ).trimmed(), p );
73
pair.second = FilterExpression( boolStr.trimmed(), p );
75
boolVars.append( pair );
78
IfNode *n = new IfNode( boolVars, linkType, p );
80
NodeList trueList = p->parse( n, QStringList() << "else" << "endif" );
81
n->setTrueList( trueList );
83
if ( p->takeNextToken().content.trimmed() == "else" ) {
84
falseList = p->parse( n, QStringList() << "endif" );
85
n->setFalseList( falseList );
86
// skip past the endif tag
88
} // else empty falseList.
94
IfNode::IfNode( QList<QPair<bool, FilterExpression > > boolVars, int linkType, QObject *parent )
96
m_boolVars( boolVars ),
97
m_linkType( linkType )
102
void IfNode::setTrueList( NodeList trueList )
104
m_trueList = trueList;
107
void IfNode::setFalseList( NodeList falseList )
109
m_falseList = falseList;
112
void IfNode::render( OutputStream *stream, Context *c )
114
// Evaluate the expression. rendering variables with the context as needed. and processing nodes recursively
115
// in either trueList or falseList as determined by booleanExpression.
117
if ( m_linkType == OrLink ) {
118
for ( int i = 0; i < m_boolVars.size(); i++ ) {
119
QPair<bool, FilterExpression> pair = m_boolVars.at( i );
120
bool negate = pair.first;
122
bool isTrue = pair.second.isTrue( c );
124
if ( isTrue != negate ) {
125
renderTrueList( stream, c );
129
// return renderFalseList(c);
131
bool renderTrue = true;
132
for ( int i = 0; i < m_boolVars.size(); i++ ) {
133
QPair<bool, FilterExpression> pair = m_boolVars.at( i );
134
bool negate = pair.first;
136
bool isTrue = pair.second.isTrue( c );
144
if (( !isTrue && !negate )
145
|| ( isTrue && negate ) ) {
151
renderTrueList( stream, c );
156
renderFalseList( stream, c );
159
void IfNode::renderTrueList( OutputStream *stream, Context *c )
161
return m_trueList.render( stream, c );
164
void IfNode::renderFalseList( OutputStream *stream, Context *c )
166
return m_falseList.render( stream, c );