Esempio n. 1
0
        public Error LoadTemplateFile(string fileName, TemplateProperties properties = null)
        {
            var error = new Error();

            try {
                string template = File.ReadAllText(fileName);
                error = LoadTemplate(template, properties);
            } catch (Exception e1) {
                error.SetError(e1);
            }

            return(error);
        }
Esempio n. 2
0
 public TemplateService()
 {
     TemplateProperties = new TemplateProperties();
 }
Esempio n. 3
0
 public TemplateService(TemplateProperties properties)
 {
     TemplateProperties = properties;
 }
Esempio n. 4
0
        public Error LoadTemplate(string templateText, TemplateProperties properties = null)
        {
            var error = new Error();

            int nestCount = 0;

            bool   bAddToPreamble  = true;
            string sectionTemplate = "";

            TemplateSection section = null;

            if (properties != null)
            {
                TemplateProperties = properties;
            }

            List <string> sections = TemplateProperties.GetTemplateSections();

            string[] templateLines = templateText.Replace("\r\n", "\n").Trim().Split('\n');

            // When we read a template, we assume that all templates are consecutive and that there
            // is no html between sections.
            // There can be 'pre-amble' html before the first section and post-amble after the last section.
            foreach (string templateLine in templateLines)
            {
                if (!string.IsNullOrEmpty(templateLine.Trim()))
                {
                    if (section != null)
                    {
                        // Inside a section, so check for end of section
                        sectionTemplate += templateLine + "\r\n";
                        if (templateLine.IndexOf("<tr") != -1)
                        {
                            nestCount++;
                        }
                        if (templateLine.IndexOf("</tr>") != -1)
                        {
                            nestCount--;
                            if (nestCount == 0)
                            {
                                section.SetTemplateText(sectionTemplate);
                                sectionTemplate = "";
                                section         = null;
                            }
                        }
                    }
                    else
                    {
                        // Not in a section, so look for a start of section
                        int foundIdx = -1;
                        for (int i = 0; i < sections.Count() && foundIdx == -1; i++)
                        {
                            if (templateLine.IndexOf(sections[i]) != -1)
                            {
                                foundIdx = i;
                            }
                        }

                        if (foundIdx != -1)
                        {
                            // Found a section start
                            nestCount = 1;
                            string sectionName = sections[foundIdx];

                            section         = AddTemplateSection(sectionName);
                            sectionTemplate = templateLine + "\r\n";
                            bAddToPreamble  = false;
                        }
                        else
                        {
                            // Section start not found, so add to pre/post amble content
                            if (bAddToPreamble)
                            {
                                preAmble += templateLine + "\r\n";
                            }
                            else
                            {
                                postAmble += templateLine + "\r\n";
                            }
                        }
                    }
                }
            }
            return(error);
        }