Beispiel #1
0
 public TemplateRenderModel(Template template)
 {
     Id        = template.Id;
     Name      = template.Name;
     CacheName = TemplateServices.GetTemplateCacheName(template.Name, template.Created, template.Updated);
     Content   = template.Content;
 }
Beispiel #2
0
 /// <summary>
 /// Check if template is modified to re-cache
 /// </summary>
 /// <param name="virtualPath"></param>
 /// <param name="virtualPathDependencies"></param>
 /// <returns></returns>
 public override string GetFileHash(string virtualPath, System.Collections.IEnumerable virtualPathDependencies)
 {
     if (IsPathIsDbTemplate(virtualPath))
     {
         var template = FindTemplate(virtualPath);
         return(TemplateServices.GetTemplateCacheName(template.Name, template.Created, template.Updated));
     }
     return(base.GetFileHash(virtualPath, virtualPathDependencies));
 }
Beispiel #3
0
        public void EmptyHtml()
        {
            TemplateServices templateServices = TemplateUtilities.CreateTestTemplateServices();

            string html = "";
            Dictionary <string, string> values = new Dictionary <string, string>();
            string actualHtml = null;

            actualHtml = templateServices.InjectTemplateValues(html, values);

            // Check result
            Assert.AreEqual(html, actualHtml);
        }
        public async Task SingleFolderSingleTemplate()
        {
            TemplateServices templateServices       = TemplateUtilities.CreateTestTemplateServices();
            Dictionary <string, Template> templates = new Dictionary <string, Template>();

            await templateServices.LoadTemplatesRecursive(new FolderMock()
            {
                Name = "Folder0", Path = "Folder0"
            }, templates);

            // Check result
            Assert.AreEqual(1, templates.Count);
            Template template = templates["template1"];
        }
Beispiel #5
0
        public void NoTemplates()
        {
            TemplateServices templateServices = TemplateUtilities.CreateTestTemplateServices();

            string rawHtml = "some content";
            Dictionary <string, Template> templates = new Dictionary <string, Template>();
            string actualHtml = null;
            Dictionary <string, string> actualValues = new Dictionary <string, string>();

            templateServices.InjectTemplateRecursive(rawHtml, templates, out actualHtml, ref actualValues);

            // Check result
            Assert.AreEqual(rawHtml, actualHtml);
            Assert.AreEqual(0, actualValues.Count);
        }
Beispiel #6
0
        public void TwoInjectionPoints()
        {
            TemplateServices templateServices = TemplateUtilities.CreateTestTemplateServices();

            string html = "some <!-- INJECT_TEMPLATE_VALUE valueId='injectionPoint0' --> content <!-- INJECT_TEMPLATE_VALUE valueId='injectionPoint1' --> text";
            Dictionary <string, string> values = new Dictionary <string, string>()
            {
                { "injectionPoint0", "injected text" }, { "injectionPoint1", "injected text2" }
            };
            string actualHtml = null;

            actualHtml = templateServices.InjectTemplateValues(html, values);

            // Check result
            Assert.AreEqual("some injected text content injected text2 text", actualHtml);
        }
Beispiel #7
0
        public void TwoInjectionPointsCurlyStartAndEnd()
        {
            TemplateServices templateServices = TemplateUtilities.CreateTestTemplateServices();

            string html = "{{{injectionPoint0}}} some content text {{{injectionPoint1}}}";
            Dictionary <string, string> values = new Dictionary <string, string>()
            {
                { "injectionPoint0", "injected text" }, { "injectionPoint1", "injected text2" }
            };
            string actualHtml = null;

            actualHtml = templateServices.InjectTemplateValues(html, values);

            // Check result
            Assert.AreEqual("injected text some content text injected text2", actualHtml);
        }
Beispiel #8
0
        public void SingleInjectionPointCurly()
        {
            TemplateServices templateServices = TemplateUtilities.CreateTestTemplateServices();

            string html = "some {{{injectionPoint0}}} content";
            Dictionary <string, string> values = new Dictionary <string, string>()
            {
                { "injectionPoint0", "injected text" }
            };
            string actualHtml = null;

            actualHtml = templateServices.InjectTemplateValues(html, values);

            // Check result
            Assert.AreEqual("some injected text content", actualHtml);
        }
Beispiel #9
0
        public void SingleTemplate()
        {
            TemplateServices templateServices = TemplateUtilities.CreateTestTemplateServices();

            string rawHtml = "some <!-- INJECT_TEMPLATE templateId='template0' --> content";
            Dictionary <string, Template> templates = TemplateUtilities.CreateTestTemplates();
            string actualHtml = null;
            Dictionary <string, string> actualValues = new Dictionary <string, string>();

            templateServices.InjectTemplateRecursive(rawHtml, templates, out actualHtml, ref actualValues);

            // Check result
            string expectedHTML = "some template0 content text content";

            Assert.AreEqual(expectedHTML, actualHtml);
            Assert.AreEqual(0, actualValues.Count);
        }
        public void UpdateTestWhenTemplateHasReference()
        {
            var faker          = new Bogus.Faker();
            int mockTemplateId = faker.Random.Number();
            var mockContext    = new Mock <SMSDb>();


            var smsHistoryList = new List <SMSHistory>
            {
                FixtureGenerator.CreateSMSHistory(mockTemplateId)
            };
            var mockSMSHistorySet = EFHelper.GetQueryableMockDbSet(smsHistoryList);

            mockContext.Setup(c => c.SMSHistoryRecords).Returns(mockSMSHistorySet.Object);

            // Set the template
            var templateList    = new List <Template>();
            var mockTemplateSet = EFHelper.GetQueryableMockDbSet(templateList);

            //I need to set the Set<Template> Template
            // because the line dbSet = DbContext.Set<T>(); on the Generic repository fails
            mockContext.Setup(m => m.Set <Template>()).Returns(mockTemplateSet.Object);
            mockContext.Setup(c => c.Templates).Returns(mockTemplateSet.Object);

            var mockICurrentUserService = new Mock <ICurrentUserService>();

            mockICurrentUserService.Setup(p => p.GetCurrentUser()).Returns("TestUser");
            // Mocking up dbFactory
            var mockdbFactory = new Mock <DbFactory <SMSDb> >();

            mockdbFactory.Setup(m => m.DBContext).Returns(mockContext.Object);
            IUnitOfWork <SMSDb> UoW = new UnitOfWork <SMSDb>(mockdbFactory.Object, mockICurrentUserService.Object);

            var target = new TemplateServices(mockdbFactory.Object);

            target.Update(new EFModel.Template()
            {
                Id = mockTemplateId
            });

            Assert.IsTrue(mockContext.Object.Templates.Any());
        }
Beispiel #11
0
        public string RenderPageTemplate(int?pageTemplateId, PageRenderModel model)
        {
            var pageTemplate = GetById(pageTemplateId);
            var config       = _templateServices.GetConfig();

            using (var templateService = new TemplateService(config))
            {
                /*
                 * Get default master template for all content
                 * This template is used for including some scripts or html for all page contents and file contents
                 */
                var defaultTemplate = GetDefaultTemplate();
                var template        = defaultTemplate.Content;
                template = CurlyBracketParser.ParseProperties(template);
                template = CurlyBracketParser.ParseRenderBody(template);

                var layout = TemplateServices.GetTemplateCacheName(defaultTemplate.Name, defaultTemplate.Created,
                                                                   defaultTemplate.Updated);
                if (Razor.Resolve(layout) == null)
                {
                    templateService.Compile(template, typeof(PageRenderModel), layout);
                }

                /*
                 * Loop all the parent template to compile and render layout
                 */
                if (pageTemplate != null)
                {
                    // Using hierarchy to load all parent templates
                    var pageTemplates =
                        _pageTemplateRepository.GetAll().Where(t => pageTemplate.Hierarchy.Contains(t.Hierarchy))
                        .OrderBy(t => t.Hierarchy)
                        .Select(t => new
                    {
                        t.Content,
                        t.Name,
                        t.Updated,
                        t.Created
                    });
                    if (pageTemplates.Any())
                    {
                        foreach (var item in pageTemplates)
                        {
                            //Convert curly bracket properties to razor syntax
                            template = CurlyBracketParser.ParseProperties(item.Content);

                            //Insert master page for child template and parsing
                            template = InsertMasterPage(template, layout);
                            template = FormatMaster(template);
                            template = templateService.Parse(template, model, null, null);
                            template = ReformatMaster(template);

                            //This used for re-cache the template
                            layout = TemplateServices.GetTemplateCacheName(item.Name, item.Created, item.Updated);

                            //Convert {RenderBody} to @RenderBody() for next rendering
                            template = CurlyBracketParser.ParseRenderBody(template);
                            if (Razor.Resolve(layout) == null)
                            {
                                templateService.Compile(template, typeof(PageRenderModel), layout);
                            }
                        }
                    }
                }
                return(template);
            }
        }