/// <summary> /// Processes the specified text as a view and sets this result body. Do not call this unless the constructor you used says to do so. /// </summary> /// <param name="viewText">The view's text.</param> /// <param name="ViewData">A ViewDataContainer containing values for expressions found within the view.</param> /// <returns></returns> public void ProcessView(string viewText, ViewDataContainer ViewData) { if (ViewData != null) { StringBuilder sb = new StringBuilder(viewText.Length); StringBuilder expressionBuffer = new StringBuilder(); ViewParseState state = ViewParseState.HTML; bool expressionStartedWithParenthesis = false; foreach (char c in viewText) { if (state == ViewParseState.HTML) { if (c == '@') { state = ViewParseState.Expression; } else { sb.Append(c); } } else if (state == ViewParseState.Expression) { if (c == '@') { if (expressionBuffer.Length == 0) { // This was a sequence of two adjacent '@' characters, which is output as one literal '@' character. state = ViewParseState.HTML; sb.Append(c); } else { // New '@' ends the previous expression and starts a new one. sb.Append(ProcessExpression(expressionBuffer, ViewData)); } } else if (expressionBuffer.Length == 0 && c == '(') { // Expressions can be within parenthesis in order // to mark their end to allow for cases like: // "@(AppRoot)images/icon.jpg" expressionStartedWithParenthesis = true; } else if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_' || c == ':') { // This character gets added to the expression. expressionBuffer.Append(c); } else { if (expressionStartedWithParenthesis && c != ')') { throw new Exception("Expression began with opening parenthesis but did not end with closing parenthesis."); } // This character ends the expression. state = ViewParseState.HTML; sb.Append(ProcessExpression(expressionBuffer, ViewData)); if (!expressionStartedWithParenthesis) { sb.Append(c); } expressionStartedWithParenthesis = false; } } } if (state == ViewParseState.Expression) { sb.Append(ProcessExpression(expressionBuffer, ViewData)); } viewText = sb.ToString(); } BodyStr = viewText; }
/// <summary> /// Processes the specified text as a view and sets this result body. /// </summary> /// <param name="viewText">The view's text.</param> /// <param name="ViewData">A ViewDataContainer containing values for expressions found within the view.</param> /// <returns></returns> public void ProcessView(string viewText, ViewDataContainer ViewData) { if (ViewData != null) { StringBuilder sb = new StringBuilder(viewText.Length); StringBuilder expressionBuffer = new StringBuilder(); ViewParseState state = ViewParseState.HTML; foreach (char c in viewText) { if (state == ViewParseState.HTML) { if (c == '@') { state = ViewParseState.Expression; } else { sb.Append(c); } } else if (state == ViewParseState.Expression) { if (c == '@') { if (expressionBuffer.Length == 0) { // This was a sequence of two adjacent '@' characters, which is output as one literal '@' character. state = ViewParseState.HTML; sb.Append(c); } else { // New '@' ends the previous expression and starts a new one. sb.Append(ProcessExpression(expressionBuffer, ViewData)); } } else if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_' || c == ':') { // This character gets added to the expression. expressionBuffer.Append(c); } else { // This character ends the expression. state = ViewParseState.HTML; sb.Append(ProcessExpression(expressionBuffer, ViewData)); sb.Append(c); } } } if (state == ViewParseState.Expression) { sb.Append(ProcessExpression(expressionBuffer, ViewData)); } viewText = sb.ToString(); } Body = Encoding.UTF8.GetBytes(viewText); }