Esempio n. 1
0
        public (string RelativePath, object Model) GenerateRenderInformation(Page page, IEnumerable <TemplateParameter> parameters = null)
        {
            if (page is null)
            {
                throw new System.ArgumentNullException(nameof(page));
            }

            parameters = parameters ?? new List <TemplateParameter>();

            string PageContent;

            lock (ViewInjectorLock)
            {
                if (ViewInjectors is null)
                {
                    StringBuilder viewInjectors = new StringBuilder();

                    viewInjectors.Append(string.Empty);

                    if (this.ServiceProvider != null)
                    {
                        foreach (Type t in TypeFactory.GetAllImplementations(typeof(IMacroProvider)))
                        {
                            if (this.ServiceProvider.GetService(t) != null)
                            {
                                viewInjectors.Append($"@inject {t.GetDeclaration()} {t.Name} {System.Environment.NewLine}");
                            }
                        }
                    }

                    ViewInjectors = viewInjectors.ToString();
                }
            }

            PageContent = ViewInjectors + System.Environment.NewLine;

            if (!string.IsNullOrWhiteSpace(page.Layout))
            {
                PageContent += "@{ Layout = \"" + page.Layout + "\"; }" + System.Environment.NewLine + System.Environment.NewLine;
            }
            else
            {
                PageContent += "@{ Layout = null; }" + System.Environment.NewLine + System.Environment.NewLine;
            }

            PageContent += page.Content;

            GeneratedTemplateInfo generatedTemplateInfo = base.GenerateTemplatePath(page, parameters, PageContent, "Content");

            return(generatedTemplateInfo.RelativePath, generatedTemplateInfo.Model);
        }
        /// <summary>
        /// Takes information required by the email template and attempts to return bound HTML representing the provided field
        /// </summary>
        /// <param name="SourceValues">A collection of object names and values to be passed into the template</param>
        /// <param name="Template">The template to use for generation</param>
        /// <param name="Field">The field/property on the email template that this call is binding (in case its not the body)</param>
        /// <returns>The Html contents of the post-bound template field</returns>
        public string RenderEmail(IEnumerable <TemplateParameter> SourceValues, IEmailTemplate Template, PropertyInfo Field)
        {
            if (Field is null)
            {
                throw new System.ArgumentNullException(nameof(Field));
            }

            string Body = Field.GetValue(Template)?.ToString();

            if (string.IsNullOrWhiteSpace(Body))
            {
                return(Body);
            }

            GeneratedTemplateInfo info = base.GenerateTemplatePath(Template, SourceValues, Body, Field.Name);

            Task <string> renderTask = this.ViewRenderService.RenderToStringAsync(info.RelativePath, info.AbsolutePath.Replace(info.RelativePath, ""), info.Model, true);

            renderTask.Wait();

            return(renderTask.Result.Trim());
        }