Example #1
0
        public IRazorView GetTemplate(RenderTemplateContext context)
        {
            IRazorView view = default(IRazorView);

            if (!viewCache.TryGetValue(context.TemplateName, out view))
            {
                string viewTemplate = TemplateLocator.LoadTemplateContent(context.TemplateName);

                IRazorCompileService complileService = new RoslynCompileService();

                CodeGenerateService codeGenerater = new CodeGenerateService();
                var generateResult = codeGenerater.Generate(context.ModelType, viewTemplate);


                if (context.GenerateResult != null)
                {
                    context.GenerateResult.Success = generateResult.Success;
                    context.GenerateResult.Errors.AddRange(
                        generateResult.ParserErrors.Select(pe => pe.ToString()));
                }
                else
                {
                    context.GenerateResult = new CodeGenerateResult()
                    {
                        Success = generateResult.Success,
                        Errors  = generateResult.ParserErrors.
                                  Select(pe => pe.ToString()).ToList()
                    };
                }

                RoslynCompileService service = new RoslynCompileService();
                var type = service.Compile(generateResult.GeneratedCode);

                if (context.BuildResult != null)
                {
                    context.BuildResult.Success = service.CompileResult.Success;
                    context.BuildResult.Errors.AddRange(service.CompileResult.Errors);
                }
                else
                {
                    context.BuildResult = service.CompileResult;
                }

                if (type == null)
                {
                    return(null);
                }

                var tb = (RazorViewTemplate)Activator.CreateInstance(type);

                view = new RazorView(tb, this, context);
                viewCache.Add(context.TemplateName, view);
            }

            view.SetContext(context);

            return(view);
        }
Example #2
0
 public string RenderView(HttpContext httpContext, string viewName, object model, DynamicDictionary viewbag)
 {
     using (var context = new RenderTemplateContext()
     {
         TemplateName = viewName,
         Path = httpContext.Request.Path,
         Model = model,
         ModelType = model?.GetType(),
         ViewBag = viewbag
     })
     {
         IRazorView view = templateService.GetTemplate(context);
         view.Render();
         this.RenderContext = context;
         return(context.ToString());
     }
 }
Example #3
0
        public void Render()
        {
            //not set writer by context , i have result of the template.
            this.Template.SetModel(Context.Model, Context.ViewBag);
            this.Template.Execute().Wait();


            if (!string.IsNullOrEmpty(this.Template.Layout))
            {
                using (var layoutContext = new RenderTemplateContext()
                {
                    TemplateName = this.Template.Layout,
                    ModelType = null,
                    ViewBag = Context.ViewBag
                })
                {
                    var layoutTemplateView = this.TemplateService.GetTemplate(layoutContext);

                    Context.IsRenderLayout = true;


                    //RenderBody is this template result ;
                    layoutTemplateView.Template.RenderBody = () => this.Template.Result;

                    layoutTemplateView.Template.SetModel(null, Context.ViewBag);

                    layoutTemplateView.Render();

                    Context.Result = layoutTemplateView.Template.Result;
                }
            }
            else
            {
                Context.Result = this.Template.Result;
            }
        }
Example #4
0
 public void SetContext(RenderTemplateContext context)
 {
     this.Context         = null;
     this.Context         = context;
     this.Template.Writer = context.Writer;
 }
Example #5
0
 public RazorView(RazorViewTemplate template, ITemplateService templateService, RenderTemplateContext context)
 {
     this.Template        = template;
     this.TemplateService = templateService;
     this.Context         = context;
 }