Ejemplo n.º 1
0
        public static TemplateScopeContext CreateScopedContext(this TemplateScopeContext scope, string template, Dictionary <string, object> scopeParams = null, bool cachePage = true)
        {
            TemplatePage dynamicPage = null;

            if (cachePage)
            {
                scope.Context.Cache.TryGetValue(template, out object value);
                dynamicPage = value as TemplatePage;
            }

            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 TemplateScopeContext(pageResult, scope.OutputStream, newScopeParams);

            return(itemScope);
        }
Ejemplo n.º 2
0
 public void GetPage(string fromVirtualPath, string virtualPath, out TemplatePage page, out TemplateCodePage codePage)
 {
     if (!TryGetPage(fromVirtualPath, virtualPath, out page, out codePage))
     {
         throw new FileNotFoundException($"Page at path was not found: '{virtualPath}'");
     }
 }
Ejemplo n.º 3
0
        public void TryGetPage(string fromVirtualPath, string virtualPath, out TemplatePage page, out TemplateCodePage codePage)
        {
            var tryExactMatch = virtualPath.IndexOf('/') >= 0; //if nested path specified, look for an exact match first

            if (tryExactMatch)
            {
                var cp = GetCodePage(virtualPath);
                if (cp != null)
                {
                    codePage = cp;
                    page     = null;
                    return;
                }

                var p = Pages.GetPage(virtualPath);
                if (p != null)
                {
                    page     = p;
                    codePage = null;
                    return;
                }
            }

            //otherwise find closest match from page.VirtualPath
            var parentPath = fromVirtualPath.IndexOf('/') >= 0
                ? fromVirtualPath.LastLeftPart('/')
                : "";

            do
            {
                var seekPath = parentPath.CombineWith(virtualPath);
                var cp       = GetCodePage(seekPath);
                if (cp != null)
                {
                    codePage = cp;
                    page     = null;
                    return;
                }

                var p = Pages.GetPage(seekPath);
                if (p != null)
                {
                    page     = p;
                    codePage = null;
                    return;
                }

                if (parentPath == "")
                {
                    break;
                }

                parentPath = parentPath.IndexOf('/') >= 0
                    ? parentPath.LastLeftPart('/')
                    : "";
            } while (true);

            throw new FileNotFoundException($"Page at path was not found: '{virtualPath}'");
        }
Ejemplo n.º 4
0
 public PageResult(TemplatePage page)
 {
     Page    = page ?? throw new ArgumentNullException(nameof(page));
     Args    = new Dictionary <string, object>();
     Filters = new List <PageTemplateFilter>();
     Options = new Dictionary <string, string>
     {
         { HttpHeaders.ContentType, MimeTypes.Html },
     };
 }
Ejemplo n.º 5
0
        public TemplatePage HtmlResolveLayout(TemplatePage page)
        {
            var isCompletePage = page.BodyContents.StartsWith("<!DOCTYPE HTML>") || page.BodyContents.StartsWithIgnoreCase("<html");

            if (isCompletePage)
            {
                return(null);
            }

            return(base.DefaultResolveLayout(page));
        }
Ejemplo n.º 6
0
 public TemplatePage DefaultResolveLayout(TemplatePage page)
 {
     page.Args.TryGetValue(TemplatePages.Layout, out object layout);
     return(page.Context.Pages.ResolveLayoutPage(page, layout as string));
 }
Ejemplo n.º 7
0
        public static void TryGetPage(this TemplateScopeContext scope, string virtualPath, out TemplatePage page, out TemplateCodePage codePage)
        {
            scope.Context.TryGetPage(scope.PageResult.VirtualPath, virtualPath, out page, out codePage);
            codePage?.Init();

            var requiresRequest = codePage as IRequiresRequest;

            if (requiresRequest != null)
            {
                var request = scope.GetValue(TemplateConstants.Request) as IRequest;
                if (request != null)
                {
                    requiresRequest.Request = request;
                }
            }
        }
Ejemplo n.º 8
0
        public static Dictionary <string, object> GetParamsWithItemBindingOnly(this TemplateScopeContext scope, string filterName, TemplatePage 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 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);
        }
Ejemplo n.º 9
0
        public static Dictionary <string, object> GetParamsWithItemBinding(this TemplateScopeContext scope, string filterName, TemplatePage 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);
        }
Ejemplo n.º 10
0
 public static async Task WritePageAsync(this TemplateScopeContext scope, TemplatePage page, TemplateCodePage codePage, Dictionary <string, object> pageParams, CancellationToken token = default(CancellationToken))
 {
     await scope.PageResult.WritePageAsync(page, codePage, scope.ScopeWithParams(pageParams), token);
 }
Ejemplo n.º 11
0
        public bool TryGetPage(string fromVirtualPath, string virtualPath, out TemplatePage page, out TemplateCodePage codePage)
        {
            var pathMapKey = nameof(GetPage) + ">" + fromVirtualPath;
            var mappedPath = GetPathMapping(pathMapKey, virtualPath);

            if (mappedPath != null)
            {
                var mappedPage = Pages.GetPage(mappedPath);
                if (mappedPage != null)
                {
                    page     = mappedPage;
                    codePage = null;
                    return(true);
                }
                RemovePathMapping(pathMapKey, mappedPath);
            }

            var tryExactMatch = virtualPath.IndexOf('/') >= 0; //if nested path specified, look for an exact match first

            if (tryExactMatch)
            {
                var cp = GetCodePage(virtualPath);
                if (cp != null)
                {
                    codePage = cp;
                    page     = null;
                    return(true);
                }

                var p = Pages.GetPage(virtualPath);
                if (p != null)
                {
                    page     = p;
                    codePage = null;
                    return(true);
                }
            }

            //otherwise find closest match from page.VirtualPath
            var parentPath = fromVirtualPath.IndexOf('/') >= 0
                ? fromVirtualPath.LastLeftPart('/')
                : "";

            do
            {
                var seekPath = parentPath.CombineWith(virtualPath);
                var cp       = GetCodePage(seekPath);
                if (cp != null)
                {
                    codePage = cp;
                    page     = null;
                    return(true);
                }

                var p = Pages.GetPage(seekPath);
                if (p != null)
                {
                    page     = p;
                    codePage = null;
                    SetPathMapping(pathMapKey, virtualPath, seekPath);
                    return(true);
                }

                if (parentPath == "")
                {
                    break;
                }

                parentPath = parentPath.IndexOf('/') >= 0
                    ? parentPath.LastLeftPart('/')
                    : "";
            } while (true);

            page     = null;
            codePage = null;
            return(false);
        }
Ejemplo n.º 12
0
        public static void TryGetPage(this TemplateScopeContext scope, string virtualPath, out TemplatePage page, out TemplateCodePage codePage)
        {
            if (scope.PageResult.Partials.TryGetValue(virtualPath, out page))
            {
                codePage = null;
                return;
            }

            scope.Context.TryGetPage(scope.PageResult.VirtualPath, virtualPath, out page, out codePage);
            codePage?.Init();

            if (codePage is IRequiresRequest requiresRequest)
            {
                if (scope.GetValue(TemplateConstants.Request) is IRequest request)
                {
                    requiresRequest.Request = request;
                }
            }
        }
Ejemplo n.º 13
0
        public void TryGetPage(string fromVirtualPath, string virtualPath, out TemplatePage page, out TemplateCodePage codePage)
        {
            var pathMapKey = $"{nameof(this.TryGetPage)}>{fromVirtualPath}";
            var mappedPath = GetPathMapping(pathMapKey, virtualPath);

            if (mappedPath != null)
            {
                var mappedPage = Pages.GetPage(mappedPath);
                if (mappedPage != null)
                {
                    page     = mappedPage;
                    codePage = null;
                    return;
                }

                RemovePathMapping(pathMapKey, mappedPath);
            }

            var tryExactMatch = virtualPath.IndexOf('/') >= 0; // if nested path specified, look for an exact match first

            if (tryExactMatch)
            {
                var cp = GetCodePage(virtualPath);
                if (cp != null)
                {
                    codePage = cp;
                    page     = null;
                    return;
                }

                var p = Pages.GetPage(virtualPath);
                if (p != null)
                {
                    page     = p;
                    codePage = null;
                    return;
                }
            }

            // otherwise find closest match from page.VirtualPath
            var parentPath = fromVirtualPath.IndexOf('/') >= 0
                ? fromVirtualPath.LastLeftPart('/')
                : string.Empty;

            do
            {
                var seekPath = parentPath.CombineWith(virtualPath);
                var cp       = GetCodePage(seekPath);
                if (cp != null)
                {
                    codePage = cp;
                    page     = null;
                    return;
                }

                var p = Pages.GetPage(seekPath);
                if (p != null)
                {
                    page     = p;
                    codePage = null;
                    SetPathMapping(pathMapKey, virtualPath, seekPath);
                    return;
                }

                if (parentPath == string.Empty)
                {
                    break;
                }

                parentPath = parentPath.IndexOf('/') >= 0
                    ? parentPath.LastLeftPart('/')
                    : string.Empty;
            }while (true);

            throw new FileNotFoundException($"Page at path was not found: '{virtualPath}'");
        }