예제 #1
0
        public void CompilePageTemplateTest()
        {
            var    errors    = new ConcurrentBag <string>();
            Page   page      = this._RepositoryService.FindPageInDb("Example", "Page1");
            string jsonModel = null;

            using (var stringCompiler = new StringCompiler())
            {
                stringCompiler.CompilePageModel(page.Model);
                if (stringCompiler.IsValid)
                {
                    jsonModel = stringCompiler.ToString();
                }
            }

            if (!string.IsNullOrEmpty(jsonModel))
            {
                page.CompileTemplate(ref errors, page.Template, jsonModel);
            }
            else
            {
                errors.Add("Error compiling Model");
            }

            Assert.IsTrue(!errors.Any());
            Assert.IsNotNull(page.CompiledTemplate);
        }
예제 #2
0
        public void CompilePageModelTest()
        {
            Page page = this._RepositoryService.FindPageInDb("Example", "Page1");

            using (var stringCompiler = new StringCompiler())
            {
                stringCompiler.CompilePageModel(page.Model);

                Assert.IsTrue(stringCompiler.IsValid);
            }
        }
예제 #3
0
        public void TestStringCompiler()
        {
            var model = "var Model = new { test = \"object\" };";

            using (var stringCompiler = new StringCompiler())
            {
                stringCompiler.CompilePageModel(model);

                Assert.IsTrue(stringCompiler.IsValid);
            }
        }
예제 #4
0
        public async Task <ActionResult> Save(PageRequest pageRequest)
        {
            var page = new Page(pageRequest);

            if (ModelState.IsValid)
            {
                if (!string.IsNullOrEmpty(page.Model))
                {
                    // compile the model from the string Model declaration in pageRequest
                    // always want to compile model before saving to catch any compile errors
                    using (var stringCompiler = new StringCompiler())
                    {
                        stringCompiler.CompilePageModel(page.Model);
                        if (stringCompiler.IsValid)
                        {
                            page.CompiledModel = stringCompiler.ToString();
                        }
                        else
                        {
                            Errors.AddRange(stringCompiler.Errors);
                        }
                    } // end using StringCompiler
                }     // end if page.Model not empty

                if (Errors.Count == 0)
                {
                    page = await _CompileTemplateAndSavePage(page, saveAsFile : pageRequest.CreateTemplateFile);
                } // end if no errors after compiling model
            }     // end if valid model state
            else
            {
                Errors.Add("Invalid parameters");
            }

            return(Json(new { Status = Errors.Count == 0, Errors, Updated = page.Updated.ToString() }));
        }
예제 #5
0
        /// <summary>
        /// Returns view with the template for the passed in name and variable. The
        /// PageTemplate model is passed to the view.
        /// </summary>
        /// <param name="name"></param>
        /// <param name="section"></param>
        /// <param name="param"></param>
        /// <param name="param2"></param>
        /// <returns></returns>
        // GET: /Section/Name
        public ActionResult View(string section, string name, string param = null, string param2 = null)
        {
            Page page = null; // will store the page once we find it
            Func <Page, bool> templateNeedsCompiled = iPage => (string.IsNullOrEmpty(iPage.CompiledTemplate) &&
                                                                (!string.IsNullOrEmpty(iPage.CompiledModel)) &&
                                                                !string.IsNullOrEmpty(iPage.Template)) ||
                                                      iPage.HasInclude;
            // templage model that will be passed to the View
            var template = new PageTemplate {
                Content = string.Empty
            };

            // if AllowCache enabled in Web.Config look for the page in cache
            if (this.AllowCache)
            {
                PageCacheModel cachedPage = this.CacheManager.FindPage(name, section, param, param2, this.QueryStringParams);
                if (cachedPage != null && cachedPage.CompiledTemplate != null)
                {
                    page = cachedPage.ToPage();
                }
            }

            // if we couldn't get the page from cache get it from the db
            if (page == null || page.CompiledTemplate == null)
            {
                page = this._repository.FindPage(section, name);
                if (page.HasParams)
                {
                    page.CompiledTemplate = null;
                }
            }

            // when page doesn't have parameters can use pre-compiled template
            var templateIsCompiled = page != null && !templateNeedsCompiled(page) && !page.HasParams;

            if (templateIsCompiled)
            {
                template.Content = page.CompiledTemplate ?? page.Template;
            }
            else if (page != null && (page.HasParams || templateNeedsCompiled(page)))
            {
                // if page has url params pass them to model and compile it
                if (page.HasParams || string.IsNullOrEmpty(page.CompiledModel))
                {
                    using (var stringCompiler = new StringCompiler())
                    {
                        stringCompiler.CompilePageModel(page.Model, param, param2);
                        if (stringCompiler.IsValid)
                        {
                            page.CompiledModel = stringCompiler.ToString();
                        }
                        else
                        {
                            this.Errors.AddRange(stringCompiler.Errors);
                        }
                    } // end using stringCompiler
                }     // end if page.HasParams and page / param are not null

                if (templateNeedsCompiled(page))
                {
                    ConcurrentBag <string> errors = this.Errors;
                    page.CompileTemplate(ref errors, page.Template, page.CompiledModel);
                }

                template.Content = page.CompiledTemplate;
            } // end else if page has paramaters

            // cache page before returning template if enabled
            if (this.AllowCache)
            {
                if (page != null && !this.CacheManager.PageCacheExists(page.Name, page.Section, param, param2, this.QueryStringParams))
                {
                    this.CacheManager.AddPage(page, param, param2);
                }
            }

            // return the page with a template if it is found
            if (!string.IsNullOrEmpty(template.Content))
            {
                return(View(template));
            }


            if (Errors.Count > 0)
            {
                return(View("~/Views/ServerError.cshtml", Errors));
            }

            // return 404 view if could not find page in db or files
            return(View("~/Views/NotFound.cshtml"));
        }