internal static string RunViewComponent(ControllerAction controllerAction, TemplateParserContext context) { ControllerClass controllerClass = controllerAction.ControllerClass; if (!controllerClass.IsActionMethod(controllerAction.ActionMethod)) { return(null); } using (ViewComponent component = controllerClass.CreateViewComponent(context)) { try { controllerClass.SetupController(component, context); object returnValue = controllerClass.Run(component, "Run", context); if (returnValue is string) { return((string)returnValue); } return(TemplateUtil.ExtractBody(component.View.Render())); } catch { component.SkipTearDown = true; throw; } } }
private static string EvalComponentExpression(ExpressionParser parser, TemplateToken token, TemplateParserContext context) { TemplateToken.ParameterizedExpression pExpr = token.ExtractParameters(); string componentName = pExpr.MainExpression; if (pExpr.MainExpression.StartsWith("(") && pExpr.MainExpression.EndsWith(")")) { componentName = parser.Evaluate <string>(pExpr.MainExpression, context, token.TokenPosition); } TemplateParserContext newContext = (TemplateParserContext)context.CreateLocal(); foreach (string varName in pExpr.Parameters.Keys) { newContext.SetLocal(varName, parser.Evaluate(pExpr.Parameters[varName], context)); } ControllerAction viewComponent = WebAppHelper.GetViewComponent(componentName); if (viewComponent == null) { throw new TemplateRenderingException("View component " + componentName + " not found", token.TokenPosition); } try { return(WebAppHelper.RunViewComponent(viewComponent, newContext)); } catch (Exception e) { throw new TemplateRenderingException("Error rendering view component " + componentName, e, token.TokenPosition); } }
internal ViewComponent CreateViewComponent(TemplateParserContext context) { ViewComponent controller = (ViewComponent)Activator.CreateInstance(_classType); controller.Initialize(this, context); return(controller); }
private TemplateParserContext(TemplateParserContext parentContext) : base(parentContext) { // Initalize(); // _viewData = parentContext.ViewData; _template = parentContext.Template; _view = parentContext.View; }
public TemplateParserContext(TemplateParserContext parentContext, Template template) : base(parentContext) { Initalize(); // _viewData = parentContext.ViewData; _template = template; _view = parentContext.View; }
public static string RenderComponent(View view, string name, params object[] parameters) { TemplateParserContext newContext = new TemplateParserContext(view, view.ViewTemplate); for (int i = 0; i < parameters.Length; i += 2) { newContext.SetLocal((string)parameters[i], parameters[i + 1], parameters[i + 1] == null ? typeof(object) : parameters[i + 1].GetType()); } return(WebAppHelper.RunViewComponent(WebAppHelper.GetViewComponent(name), newContext)); }
internal void TearDownController(Controller controller, TemplateParserContext context) { try { foreach (MethodInfo method in _teardownMethods) { method.Invoke(controller, WebAppHelper.CreateParameters(method, context)); } } catch (TargetInvocationException ex) { throw ExceptionHelper.ResolveTargetInvocationException(ex); } }
protected override CompiledTemplate OnEvalParseFile(ExpressionParser parser, TemplateParser templateParser, string fileName, TemplateToken token, IParserContext context, out Dictionary <string, IValueWithType> parameters) { TemplateParserContext parserContext = (TemplateParserContext)context; TemplateToken.ParameterizedExpression pExpr = token.ExtractParameters(); parameters = new Dictionary <string, IValueWithType>(StringComparer.InvariantCultureIgnoreCase); foreach (KeyValuePair <string, string> var in pExpr.Parameters) { parameters[var.Key] = parser.Evaluate(var.Value, context); } return(parserContext.Template.CompileSubTemplate(parser.Evaluate <string>(pExpr.MainExpression))); }
protected override string OnEvalExpression(ExpressionParser parser, TemplateToken token, IParserContext context) { TemplateParserContext parserContext = (TemplateParserContext)context; switch (token.TokenId) { case TokenID.Control: return(EvalControlExpression(parser, token, parserContext)); case TokenID.Component: return(EvalComponentExpression(parser, token, parserContext)); case TokenID.Translation: return(EvalTranslationExpression(parser, token, parserContext)); default: return(EvalExpression(parser, token, parserContext)); } }
internal void SetupController(Controller controller, TemplateParserContext context) { try { foreach (MethodInfo method in _setupMethods) { method.Invoke(controller, WebAppHelper.CreateParameters(method, context)); } } catch (TargetInvocationException ex) { throw ExceptionHelper.ResolveTargetInvocationException(ex); } RegisterAjaxMethods(controller); }
internal static object[] CreateParameters(MethodInfo method, TemplateParserContext context) { ParameterInfo[] parameters = method.GetParameters(); object[] parameterValues = new object[parameters.Length]; int i = 0; foreach (ParameterInfo parameter in parameters) { ClientDataAttribute[] attributes = (ClientDataAttribute[])parameter.GetCustomAttributes(typeof(ClientDataAttribute), true); ClientDataAttribute mapAttribute; if (attributes.Length > 0) { mapAttribute = attributes[0]; } else { mapAttribute = new GetOrPostAttribute(parameter.Name); } object value; Type type; if (context != null && context.Get(parameter.Name, out value, out type)) { parameterValues[i++] = TypeHelper.ConvertType(value, parameter.ParameterType); } else { parameterValues[i++] = GetClientValue(mapAttribute, parameter.ParameterType); //TODO: add IObjectBinder support for complex objects } } return(parameterValues); }
public object Run(Controller controller, string methodName, TemplateParserContext context) { ActionMethod actionMethod = _actionMethods[methodName]; if (actionMethod.DefaultLayoutName != null) { controller.View.LayoutName = actionMethod.DefaultLayoutName; } if (actionMethod.DefaultViewName != null) { controller.View.ViewName = actionMethod.DefaultViewName; } object returnValue = actionMethod.Invoke(controller, context); if (returnValue is StringBuilder) { return(returnValue.ToString()); } return(returnValue); }
private static string EvalControlExpression(ExpressionParser parser, TemplateToken token, TemplateParserContext context) { int colon1 = token.Text.LastIndexOf(':'); int colon2 = -1; if (colon1 > 0) { colon2 = token.Text.LastIndexOf(':', colon1 - 1); } string expr = token.Text; string className = null; string classError = null; if (colon2 >= 0) { classError = expr.Substring(colon1 + 1).Trim(); className = expr.Substring(colon2 + 1, colon1 - colon2 - 1); expr = expr.Substring(0, colon2).Trim(); } else if (colon1 >= 0) { className = expr.Substring(colon1 + 1).Trim(); expr = expr.Substring(0, colon1).Trim(); } Control control = parser.Evaluate <Control>(expr, context, token.TokenPosition); if (control != null) { string html = control.Render(context.View, className, classError); return(context.View.ParseTranslations(html)); } else { return("[?[" + expr + "]?]"); } }
private static string EvalExpression(ExpressionParser parser, TemplateToken token, TemplateParserContext context) { char q = token.TokenId == TokenID.ExpressionNew ? '`' : ':'; int quoteIdx = token.Text.LastIndexOf(q); string expr = quoteIdx >= 0 ? token.Text.Substring(0, quoteIdx).Trim() : token.Text; IValueWithType result = parser.Evaluate(expr, context, token.TokenPosition); if (token.TokenType == TemplateTokenType.Statement) { return(""); } if (result.Value is Control) { return(context.View.ParseTranslations(((Control)result.Value).Render(context.View))); } string fmt = quoteIdx >= 0 ? token.Text.Substring(quoteIdx + 1).Trim() : null; if (fmt != null) { return(String.Format(WebAppConfig.FormatProvider, "{0:" + fmt + "}", result.Value)); } else if (result.Value is string) { return((string)result.Value); } else { return(result.Value == null ? "" : Convert.ToString(result.Value, WebAppConfig.FormatProvider)); } }
private static string EvalTranslationExpression(ExpressionParser parser, TemplateToken token, TemplateParserContext context) { int parenIdx = token.Text.IndexOf('('); string tag = token.Text; string parameters = null; if (parenIdx > 0) { parameters = token.Text.Substring(parenIdx + 1); tag = token.Text.Substring(0, parenIdx).Trim(); } string translation = TranslationHelper.GetTranslation(context.View.ViewName, tag); if (translation == null) { return("{?{" + tag + "}?}"); } if (parameters != null) { IParserContext localContext = context.CreateLocal(); localContext.Set("__Translation__", translation); translation = parser.Evaluate <string>("string.Format(__Translation__," + parameters, localContext, token.TokenPosition); } return(translation); }
public object Invoke(object target, TemplateParserContext parserContext) { return(Invoke(target, WebAppHelper.CreateParameters(_methodInfo, parserContext))); }