コード例 #1
0
        /// <summary>
        /// Parses the foreach. El formato es {{ foreach .posts -> post[, i] }}
        /// Donde .posts es el IEnumerable que se itera, post es cada elemento en la iteración y i es el índice opcional.
        /// </summary>
        static IRenderizable ParseForeach(Token currentToken, List <Token> tokens, ref int index, Template mainTemplate)
        {
            var item = new Foreach();

            if (tokens.Count <= index)
            {
                throw new TemplateException(string.Format("Invalid foreach at: {0}", currentToken.Position));
            }

            // Parsear el nombre del elemento de la iteración
            var nextToken = tokens [index];

            if (nextToken.Type != TokenType.Parameter)
            {
                throw new TemplateException(string.Format("Invalid if foreach at: {0}. Iterate name not found", currentToken.Position));
            }

            item.IterateKey = nextToken.Value.Trim(argumentSeparators);
            index++;

            // si hay una coma para separar, ignorarla
            nextToken = tokens [index];
            if (nextToken.Type == TokenType.Parameter && nextToken.Value.Trim() == ",")
            {
                index++;
                nextToken = tokens [index];
            }

            // Parsear el indice de la iteración (es opcional)
            if (nextToken.Type == TokenType.Parameter && nextToken.Value.Trim() != "in")
            {
                item.IndexKey = nextToken.Value.Trim(argumentSeparators);
                index++;
            }

            // Parsear el separador "in"
            nextToken = tokens [index];
            if (nextToken.Type != TokenType.Parameter && nextToken.Value.Trim() != "in")
            {
                throw new TemplateException(string.Format("Invalid if foreach at: {0}. Iterate separator 'in' not found", currentToken.Position));
            }

            index++;
            nextToken = tokens [index];
            if (nextToken.Type != TokenType.Parameter)
            {
                throw new TemplateException(string.Format("Invalid foreach at: {0}. Model not found", currentToken.Position));
            }

            item.ModelKey = nextToken.Value.Trim();
            index++;

            item.Body = Parse(mainTemplate, tokens, ref index);

            return(item);
        }
コード例 #2
0
ファイル: Parser.cs プロジェクト: scorredoira/Sfx.Templates
		/// <summary>
		/// Parses the foreach. El formato es {{ foreach .posts -> post[, i] }}
		/// Donde .posts es el IEnumerable que se itera, post es cada elemento en la iteración y i es el índice opcional.
		/// </summary>
		static IRenderizable ParseForeach (Token currentToken, List<Token> tokens, ref int index, Template mainTemplate)
		{
			var item = new Foreach ();

			if (tokens.Count <= index)
			{
				throw new TemplateException(string.Format("Invalid foreach at: {0}", currentToken.Position));
			}

			// Parsear el nombre del elemento de la iteración
			var nextToken = tokens [index];
			if (nextToken.Type != TokenType.Parameter)
			{
				throw new TemplateException(string.Format("Invalid if foreach at: {0}. Iterate name not found", currentToken.Position));
			}

			item.IterateKey = nextToken.Value.Trim(argumentSeparators);
			index++;

			// si hay una coma para separar, ignorarla
			nextToken = tokens [index];
			if (nextToken.Type == TokenType.Parameter && nextToken.Value.Trim() == ",")
			{
				index++;
				nextToken = tokens [index];
			}

			// Parsear el indice de la iteración (es opcional)
			if (nextToken.Type == TokenType.Parameter && nextToken.Value.Trim() != "in")
			{
				item.IndexKey = nextToken.Value.Trim(argumentSeparators);
				index++;
			}

			// Parsear el separador "in"
			nextToken = tokens [index];
			if (nextToken.Type != TokenType.Parameter && nextToken.Value.Trim() != "in")
			{
				throw new TemplateException(string.Format("Invalid if foreach at: {0}. Iterate separator 'in' not found", currentToken.Position));
			}
			
			index++;
			nextToken = tokens [index];
			if (nextToken.Type != TokenType.Parameter)
			{
				throw new TemplateException(string.Format("Invalid foreach at: {0}. Model not found", currentToken.Position));
			}

			item.ModelKey = nextToken.Value.Trim ();
			index++;

			item.Body = Parse(mainTemplate, tokens, ref index);
            
			return item;
        }