/// <summary> /// Tries the get page. /// </summary> /// <param name="scope">The scope.</param> /// <param name="virtualPath">The virtual path.</param> /// <param name="page">The page.</param> /// <param name="codePage">The code page.</param> /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns> public static bool TryGetPage(this ScriptScopeContext scope, string virtualPath, out SharpPage page, out SharpCodePage codePage) { if (scope.PageResult.Partials.TryGetValue(virtualPath, out page)) { codePage = null; return(true); } if (!scope.Context.TryGetPage(scope.PageResult.VirtualPath, virtualPath, out page, out codePage)) { return(false); } codePage?.Init(); if (codePage is IRequiresRequest requiresRequest) { if (scope.GetValue(ScriptConstants.Request) is IRequest request) { requiresRequest.Request = request; } } return(true); }
/// <summary> /// Creates the scoped context. /// </summary> /// <param name="scope">The scope.</param> /// <param name="template">The template.</param> /// <param name="scopeParams">The scope parameters.</param> /// <param name="cachePage">if set to <c>true</c> [cache page].</param> /// <returns>ScriptScopeContext.</returns> public static ScriptScopeContext CreateScopedContext(this ScriptScopeContext scope, string template, Dictionary <string, object> scopeParams = null, bool cachePage = true) { SharpPage dynamicPage = null; if (cachePage) { scope.Context.Cache.TryGetValue(template, out object value); dynamicPage = value as SharpPage; } if (dynamicPage == null) { dynamicPage = scope.Context.OneTimePage(template); if (cachePage) { scope.Context.Cache[template] = dynamicPage; } } var newScopeParams = new Dictionary <string, object>(scope.ScopedParams); scopeParams.Each((key, val) => newScopeParams[key] = val); var pageResult = scope.PageResult.Clone(dynamicPage).Init().Result; var itemScope = new ScriptScopeContext(pageResult, scope.OutputStream, newScopeParams); return(itemScope); }
/// <summary> /// HTMLs the resolve layout. /// </summary> /// <param name="page">The page.</param> /// <returns>SharpPage.</returns> public SharpPage HtmlResolveLayout(SharpPage page) { var isCompletePage = page.BodyContents.Span.StartsWithIgnoreCase("<!DOCTYPE HTML>".AsSpan()) || page.BodyContents.Span.StartsWithIgnoreCase("<html".AsSpan()); if (isCompletePage) { return(null); } return(base.DefaultResolveLayout(page)); }
public static PageResult ToPageResult(this PropertyValidatorContext context, SharpPage page) { var to = new PageResult(page) { Args = { [ScriptConstants.It] = context.PropertyValue, [ScriptConstants.Field] = context.PropertyName, } }; return(to); }
public ScriptConditionValidator(SharpPage code) : base(new LanguageStringSource(nameof(PredicateValidator))) { Code = code; }
public TemplateCodePageHandler(SharpCodePage page, SharpPage layoutPage = null) { this.page = page; this.layoutPage = layoutPage; }
public TemplatePageHandler(SharpPage page, SharpPage layoutPage = null) { this.RequestName = !string.IsNullOrEmpty(page.VirtualPath) ? page.VirtualPath : nameof(TemplatePageHandler); this.Page = page; this.LayoutPage = layoutPage; }
/// <summary> /// Gets the parameters with item binding only. /// </summary> /// <param name="scope">The scope.</param> /// <param name="filterName">Name of the filter.</param> /// <param name="page">The page.</param> /// <param name="scopedParams">The scoped parameters.</param> /// <param name="itemBinding">The item binding.</param> /// <returns>Dictionary<System.String, System.Object>.</returns> /// <exception cref="System.NotSupportedException">'it' option in filter '{filterName}' should contain the name to bind to but contained a '{bindingName.GetType().Name}' instead</exception> public static Dictionary <string, object> GetParamsWithItemBindingOnly(this ScriptScopeContext scope, string filterName, SharpPage page, object scopedParams, out string itemBinding) { var pageParams = scope.AssertOptions(filterName, scopedParams); itemBinding = pageParams.TryGetValue("it", out object bindingName) && bindingName is string binding ? binding : "it"; if (bindingName != null && bindingName is not string) { throw new NotSupportedException($"'it' option in filter '{filterName}' should contain the name to bind to but contained a '{bindingName.GetType().Name}' instead"); } // page vars take precedence if (page != null && page.Args.TryGetValue("it", out object pageBinding)) { itemBinding = (string)pageBinding; } return(pageParams); }
/// <summary> /// Gets the parameters with item binding. /// </summary> /// <param name="scope">The scope.</param> /// <param name="filterName">Name of the filter.</param> /// <param name="page">The page.</param> /// <param name="scopedParams">The scoped parameters.</param> /// <param name="itemBinding">The item binding.</param> /// <returns>Dictionary<System.String, System.Object>.</returns> public static Dictionary <string, object> GetParamsWithItemBinding(this ScriptScopeContext scope, string filterName, SharpPage page, object scopedParams, out string itemBinding) { var scopeParams = scope.GetParamsWithItemBindingOnly(filterName, page, scopedParams, out itemBinding); scopeParams.Each((key, val) => scope.ScopedParams[key] = val); return(scopeParams); }
/// <summary> /// Defaults the resolve layout. /// </summary> /// <param name="page">The page.</param> /// <returns>SharpPage.</returns> public SharpPage DefaultResolveLayout(SharpPage page) { page.Args.TryGetValue(SharpPages.Layout, out object layout); return(page.Context.Pages.ResolveLayoutPage(page, layout as string)); }
/// <summary> /// Writes the asynchronous. /// </summary> /// <param name="scope">The scope.</param> /// <param name="block">The block.</param> /// <param name="token">The cancellation token that can be used by other objects or threads to receive notice of cancellation.</param> /// <returns>Task.</returns> public override Task WriteAsync(ScriptScopeContext scope, PageBlockFragment block, CancellationToken token) { // block.Argument key is unique to exact memory fragment, not string equality // Parse into AST once for all Page Results var invokerCtx = (Tuple <string, StaticMethodInvoker>)scope.Context.CacheMemory.GetOrAdd(block.Argument, _ => { var literal = block.Argument.Span.ParseVarName(out var name); var strName = name.ToString(); literal = literal.AdvancePastWhitespace(); literal = literal.AdvancePastWhitespace(); var args = TypeConstants.EmptyStringArray; if (!literal.IsEmpty) { literal.ParseArgumentsList(out var argIdentifiers); args = new string[argIdentifiers.Count]; for (var i = 0; i < argIdentifiers.Count; i++) { args[i] = argIdentifiers[i].Name; } } StaticMethodInvoker invoker = null; // Allow recursion by initializing lazy Delegate object LazyInvoker(object instance, object[] paramValues) { if (invoker == null) { throw new NotSupportedException($"Uninitialized function '{strName}'"); } return(invoker(instance, paramValues)); } invoker = paramValues => { scope.PageResult.StackDepth++; try { var page = new SharpPage(Context, block.Body); var pageResult = new PageResult(page) { Args = { [strName] = (MethodInvoker)LazyInvoker }, StackDepth = scope.PageResult.StackDepth }; var len = Math.Min(paramValues.Length, args.Length); for (int i = 0; i < len; i++) { var paramValue = paramValues[i]; pageResult.Args[args[i]] = paramValue; } if (pageResult.EvaluateResult(out var returnValue)) { return(returnValue); } return(IgnoreResult.Value); } finally { scope.PageResult.StackDepth--; } };