예제 #1
0
        /// <summary>
        /// Parses a template with a given <paramref name="key" />
        /// </summary>
        /// <param name="key">Key used to resolve a template</param>
        /// <param name="model">Template model</param>
        /// <param name="modelType">Type of the model</param>
        /// <param name="viewBag">Dynamic ViewBag (can be null)</param>
        /// <returns>Returns parsed string</returns>
        /// <remarks>Result is stored in cache</remarks>
        public string Parse(string key, object model, Type modelType, ExpandoObject viewBag)
        {
            PageLookupResult result = pageLookup.GetPage(key);

            if (!result.Success)
            {
                throw new RazorLightException($"Can't find a view with a specified key ({key})");
            }

            var pageContext = new PageContext(viewBag)
            {
                ModelTypeInfo = new ModelTypeInfo(modelType)
            };

            foreach (var viewStartPage in result.ViewStartEntries)
            {
                pageContext.ViewStartPages.Add(viewStartPage.PageFactory());
            }

            TemplatePage page = result.ViewEntry.PageFactory();

            page.PageContext = pageContext;

            return(RunTemplate(page, model));
        }
예제 #2
0
        /// <summary>
        /// Includes the template with the specified key
        /// </summary>
        /// <param name="key">Key used to resolve a template</param>
        /// <param name="model">Template model</param>
        public async Task IncludeAsync(string key, object model = null)
        {
            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentNullException(nameof(key));
            }

            if (this.PageLookup == null)
            {
                throw new RazorLightException("Can't locate a page as PageLookup is not set");
            }

            PageLookupResult pageResult = PageLookup.GetPage(key);

            if (pageResult.Success)
            {
                TemplatePage page = pageResult.ViewEntry.PageFactory();
                page.PageContext = new PageContext(this.PageContext.ViewBag)
                {
                    Writer = this.PageContext.Writer
                };

                if (model != null)
                {
                    var modelTypeInfo = new ModelTypeInfo(model.GetType());
                    page.PageContext.ModelTypeInfo = modelTypeInfo;

                    object pageModel = modelTypeInfo.CreateTemplateModel(model);
                    page.SetModel(pageModel);
                }

                await page.ExecuteAsync();
            }
        }
예제 #3
0
        /// <summary>
        /// Runs a template, renders a Layout pages and sections.
        /// </summary>
        /// <param name="page">Page to run</param>
        /// <param name="model">Mode of the page</param>
        public string RunTemplate(TemplatePage page, object model)
        {
            object pageModel = page.PageContext.ModelTypeInfo.CreateTemplateModel(model);

            page.SetModel(pageModel);
            page.Path = page.PageContext.ExecutingFilePath;

            using (var writer = new StringWriter())
            {
                page.PageContext.Writer = writer;

                using (var renderer = new PageRenderer(page, pageLookup))
                {
                    renderer.ViewStartPages.AddRange(page.PageContext.ViewStartPages);
                    renderer.PreRenderCallbacks.AddRange(Configuration.PreRenderCallbacks);
                    renderer.RenderAsync(page.PageContext).GetAwaiter().GetResult();
                    return(writer.ToString());
                }
            }
        }