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/>.
25
#include "exception.h"
31
RangeNodeFactory::RangeNodeFactory()
36
Grantlee::Node* RangeNodeFactory::getNode( const QString& tagContent, Parser* p ) const
38
QStringList expr = smartSplit( tagContent );
41
int numArgs = expr.size();
45
throw Grantlee::Exception( TagSyntaxError, "'range' tag requires at least three arguments" );
48
if ( expr.at( numArgs - 2 ) != "as" ) {
49
throw Grantlee::Exception( TagSyntaxError, "Invalid arguments to 'range' tag" );
53
const QString name = ( numArgs > 2 ) ? expr.at( numArgs - 1 ) : QString();
61
n = new RangeNode( name, FilterExpression( "0", p ), FilterExpression( expr.at( 0 ), p ), p );
64
n = new RangeNode( name, FilterExpression( expr.at( 0 ), p ), FilterExpression( expr.at( 1 ), p ), p );
67
n = new RangeNode( name, FilterExpression( expr.at( 0 ), p ), FilterExpression( expr.at( 1 ), p ), FilterExpression( expr.at( 2 ), p ), p );
73
NodeList list = p->parse( n, "endrange" );
76
n->setNodeList( list );
80
RangeNode::RangeNode( const QString &name, const FilterExpression &startExpression, const FilterExpression &stopExpression, QObject* parent )
83
m_startExpression( startExpression ),
84
m_stopExpression( stopExpression )
88
RangeNode::RangeNode( const QString &name, const FilterExpression &startExpression, const FilterExpression &stopExpression, const FilterExpression &stepExpression, QObject* parent )
91
m_startExpression( startExpression ),
92
m_stopExpression( stopExpression ),
93
m_stepExpression( stepExpression )
97
void RangeNode::setNodeList( NodeList list )
102
void RangeNode::render( OutputStream *stream, Context* c )
108
start = m_startExpression.resolve( c ).toInt();
109
stop = m_stopExpression.resolve( c ).toInt();
111
if ( m_stepExpression.isValid() ) {
112
step = m_stepExpression.resolve( c ).toInt();
117
const bool insertContext = !m_name.isEmpty();
119
Q_ASSERT( start < stop );
122
for ( int i = start; i < stop; i += step ) {
123
if ( insertContext ) {
125
c->insert( m_name, i );
127
m_list.render( stream, c );