public IContent WritePage(ISource source, IPageData pageData, OutputData outputData)
        {
            IPageTemplate template = CreatePageTemplate(PageTemplateName);

            if (Scripts == null)
            {
                Scripts = new UserScript(null);
            }
            object pageDataObj = PageData;

            if (pageDataObj == null)
            {
                pageDataObj = template.GetDefaultPageData(source, pageData, outputData);
            }
            if (pageDataObj != null)
            {
                OnSetPageData(new PageDataEventArgs(source, pageData, outputData, pageDataObj));
            }

            object model = WebRazorUtil.GetModel(outputData);

            var    viewBag = WebRazorUtil.GetNewViewBag(pageData, fMetaData, Scripts, pageDataObj);
            string content = Execute(template, PageTemplateName, ModelCreator, RazorFile,
                                     WebRazorUtil.GetModel(outputData), viewBag, source, pageData, outputData);

            return(new SimpleContent(ContentTypeConst.HTML, content));
        }
        protected override void BuildPageTemplateRecursive(IPageTemplate pageTemplate, string theme, RequestContext context, StringBuilder output, CursorCollection placeHolders, DirectiveCollection directives, List<IControlsContainer> controlConatiners)
        {
            controlConatiners.Add(pageTemplate);

            if (!String.IsNullOrEmpty(pageTemplate.MasterPage))
            {
                this.BuildWithMasterPage(pageTemplate.MasterPage, context, output, placeHolders, directives);
                return;
            }

            if (!String.IsNullOrEmpty(pageTemplate.ExternalPage))
            {
                this.BuildWithExternalPage(pageTemplate.ExternalPage, context, output, placeHolders, directives);
                return;
            }

            if (this.BuildTemplateFromPresentationData(pageTemplate.Presentation, theme, output, placeHolders, directives, context))
                return;

            if (pageTemplate.ParentTemplate != null)
            {
                BuildPageTemplateRecursive(pageTemplate.ParentTemplate, theme, context, output, placeHolders, directives, controlConatiners);
                return;
            }

            var defaultRootTemplate = new RootTemplate();
            defaultRootTemplate.FromString(ControlUtilities.GetSitefinityTextResource(Constants.DefaultFrontendPageTemplate));
            RootTemplate result = this.GetRootTemplate(defaultRootTemplate, context, theme);
            this.ProcessStringTemplate(result.Template, output, placeHolders, directives);
        }
Esempio n. 3
0
        /// <summary>
        /// Gets the template framework.
        /// </summary>
        /// <param name="template">The template.</param>
        /// <returns></returns>
        public static PageTemplateFramework GetTemplateFramework(this IPageTemplate template)
        {
            if (template is IFrameworkSpecificPageTemplate)
            {
                return((template as IFrameworkSpecificPageTemplate).Framework);
            }

            return(PageTemplateFramework.Hybrid);
        }
        /// <summary>
        /// Binds placeholders' content to the related properties on a template definition
        /// </summary>
        /// <param name="template">The template.</param>
        /// <param name="pageContentToRender">The page rendering job.</param>
        /// <param name="placeholderProperties">The placeholder properties.</param>
        /// <param name="functionContextContainer">The function context container, if not null, nested functions fill be evaluated.</param>
        public static void BindPlaceholders(IPageTemplate template,
                                            PageContentToRender pageContentToRender,
                                            IDictionary <string, PropertyInfo> placeholderProperties,
                                            FunctionContextContainer functionContextContainer)
        {
            Verify.ArgumentNotNull(template, "template");
            Verify.ArgumentNotNull(pageContentToRender, "pageContentToRender");
            Verify.ArgumentNotNull(placeholderProperties, "placeholderProperties");

            foreach (var placeholderContent in pageContentToRender.Contents)
            {
                string placeholderId = placeholderContent.PlaceHolderId;

                if (!placeholderProperties.ContainsKey(placeholderId))
                {
                    continue;
                }

                XhtmlDocument placeholderXhtml = PageRenderer.ParsePlaceholderContent(placeholderContent);

                if (functionContextContainer != null)
                {
                    bool allFunctionsExecuted;

                    using (Profiler.Measure($"Evaluating placeholder '{placeholderId}'"))
                    {
                        allFunctionsExecuted = PageRenderer.ExecuteCacheableFunctions(placeholderXhtml.Root, functionContextContainer);
                    }

                    if (allFunctionsExecuted)
                    {
                        using (Profiler.Measure("Normalizing XHTML document"))
                        {
                            PageRenderer.NormalizeXhtmlDocument(placeholderXhtml);
                        }
                    }
                }

                PageRenderer.ResolveRelativePaths(placeholderXhtml);

                PropertyInfo property = placeholderProperties[placeholderId];

                if (!property.ReflectedType.IsInstanceOfType(template))
                {
                    string propertyName = property.Name;
                    property = template.GetType().GetProperty(property.Name);
                    Verify.IsNotNull(property, "Failed to find placeholder property '{0}'", propertyName);
                }

                property.SetValue(template, placeholderXhtml);
            }
        }
        private static string Execute(object model, dynamic viewBag, PageTemplateInitData initData,
                                      string razorFile, string layout, IPageTemplate pageTemplate, ISource source,
                                      IInputData input, OutputData outputData)
        {
            string engineName = pageTemplate.GetEngineName(source, input, outputData);

            IRazorEngine engine = RazorEnginePlugInFactory.CreateRazorEngine(engineName);

            string content = Task.Run(async()
                                      => await RazorExtension.CompileRenderWithLayoutAsync(engine, razorFile,
                                                                                           layout, model, initData, viewBag)).GetAwaiter().GetResult();

            return(content);
        }
Esempio n. 6
0
        /// <summary>
        /// Determines whether duplicate ST Items are allowed on this page.
        /// </summary>
        /// <remarks>
        /// Used in Legacy Model Builder Pipeline (DD4T-based)
        /// </remarks>
        private static bool GetAllowDuplicatesOnSamePage(IPageTemplate pageTemplate, ILocalization localization)
        {
            string allowDuplicates = null;

            if ((pageTemplate?.MetadataFields != null) && pageTemplate.MetadataFields.ContainsKey(AllowDuplicatesFieldName))
            {
                allowDuplicates = pageTemplate.MetadataFields[AllowDuplicatesFieldName].Value;
            }

            if (string.IsNullOrEmpty(allowDuplicates) || allowDuplicates.Equals(UseConfigValue, StringComparison.OrdinalIgnoreCase))
            {
                return(GetAllowDuplicatesFromConfig(localization));
            }

            return(Convert.ToBoolean(allowDuplicates));
        }
        /// <summary>
        /// Binds placeholders' content to the related properties on a template definition
        /// </summary>
        /// <param name="template">The template.</param>
        /// <param name="pageContentToRender">The page rendering job.</param>
        /// <param name="placeholderProperties">The placeholder properties.</param>
        /// <param name="functionContextContainer">The function context container, if not null, nested functions fill be evaluated.</param>
        public static void BindPlaceholders(IPageTemplate template,
                                            PageContentToRender pageContentToRender,
                                            IDictionary <string, PropertyInfo> placeholderProperties,
                                            FunctionContextContainer functionContextContainer)
        {
            Verify.ArgumentNotNull(template, "template");
            Verify.ArgumentNotNull(pageContentToRender, "pageContentToRender");
            Verify.ArgumentNotNull(placeholderProperties, "placeholderProperties");

            foreach (var placeholderContent in pageContentToRender.Contents)
            {
                string placeholderId = placeholderContent.PlaceHolderId;

                if (!placeholderProperties.ContainsKey(placeholderId))
                {
                    continue;
                }

                XhtmlDocument placeholderXhtml = PageRenderer.ParsePlaceholderContent(placeholderContent);

                if (functionContextContainer != null)
                {
                    using (Profiler.Measure("Evaluating placeholder '{0}'".FormatWith(placeholderId)))
                    {
                        PageRenderer.ExecuteEmbeddedFunctions(placeholderXhtml.Root, functionContextContainer);
                    }

                    PageRenderer.NormalizeXhtmlDocument(placeholderXhtml);
                }

                PageRenderer.ResolveRelativePaths(placeholderXhtml);

                PropertyInfo property = placeholderProperties[placeholderId];

                if (!property.ReflectedType.IsAssignableFrom(template.GetType()))
                {
                    string propertyName = property.Name;
                    property = template.GetType().GetProperty(property.Name);
                    Verify.IsNotNull(property, "Failed to find placeholder property '{0}'", propertyName);
                }

                property.SetValue(template, placeholderXhtml, new object[0]);
            }
        }
        private static string Execute(IPageTemplate template, string pageTemplateName, string modelCreator,
                                      string localRazorFile, object model, dynamic viewBag, ISource source,
                                      IInputData input, OutputData outputData)
        {
            string templateFile           = template.GetTemplateFile(source, input, outputData);
            PageTemplateInitData initData = new PageTemplateInitData(pageTemplateName, modelCreator);
            string razorFile;
            string layout;

            if (string.IsNullOrEmpty(localRazorFile))
            {
                razorFile = templateFile;
                layout    = null;
            }
            else
            {
                razorFile = localRazorFile;
                layout    = templateFile;
            }
            return(Execute(model, viewBag, initData, razorFile, layout, template, source, input, outputData));
        }
        /// <summary>
        /// Determines whether duplicate ST Items are allowed on this page.
        /// </summary>
        private static bool GetAllowDuplicatesOnSamePage(IPageTemplate pageTemplate, Localization localization)
        {
            const string allowDuplicatesFieldName = "allowDuplicationOnSamePage";
            const string allowDuplicatesConfig    = "smarttarget.allowDuplicationOnSamePageConfig";

            string allowDuplicates = null;

            if (pageTemplate != null && pageTemplate.MetadataFields != null && pageTemplate.MetadataFields.ContainsKey(allowDuplicatesFieldName))
            {
                allowDuplicates = pageTemplate.MetadataFields[allowDuplicatesFieldName].Value;
            }

            if (String.IsNullOrEmpty(allowDuplicates) || allowDuplicates.Equals("Use core configuration", StringComparison.OrdinalIgnoreCase))
            {
                allowDuplicates = localization.GetConfigValue(allowDuplicatesConfig);
                if (String.IsNullOrEmpty(allowDuplicates))
                {
                    allowDuplicates = "true";
                }
            }

            return(Convert.ToBoolean(allowDuplicates));
        }
 public void InvokeBuildPageTemplateRecursive(IPageTemplate pageTemplate, string theme, RequestContext context, StringBuilder output, CursorCollection placeHolders, DirectiveCollection directives, List<IControlsContainer> controlConatiners)
 {
     this.BuildPageTemplateRecursive(pageTemplate, theme, context, output, placeHolders, directives, controlConatiners);
 }
Esempio n. 11
0
        /// <summary>
        /// Determines whether duplicate ST Items are allowed on this page.
        /// </summary>
        private static bool GetAllowDuplicatesOnSamePage(IPageTemplate pageTemplate, Localization localization)
        {
            const string allowDuplicatesFieldName = "allowDuplicationOnSamePage";
            const string allowDuplicatesConfig = "smarttarget.allowDuplicationOnSamePageConfig";

            string allowDuplicates = null;
            if (pageTemplate != null && pageTemplate.MetadataFields != null && pageTemplate.MetadataFields.ContainsKey(allowDuplicatesFieldName))
            {
                allowDuplicates = pageTemplate.MetadataFields[allowDuplicatesFieldName].Value;
            }

            if (String.IsNullOrEmpty(allowDuplicates) || allowDuplicates.Equals("Use core configuration", StringComparison.OrdinalIgnoreCase))
            {
                allowDuplicates = localization.GetConfigValue(allowDuplicatesConfig);
                if (String.IsNullOrEmpty(allowDuplicates))
                {
                    allowDuplicates = "true";
                }
            }

            return Convert.ToBoolean(allowDuplicates);
        }
        /// <summary>
        /// Builds a page template descriptor. Extracts template's properties and content placeholder properties.
        /// </summary>
        /// <param name="templateDefinition">The template definition.</param>
        /// <param name="descriptorConstructor">The descriptor constructor.</param>
        /// <param name="placeholderProperties">The placeholder properties.</param>
        /// <returns></returns>
        public static DescriptorType BuildPageTemplateDescriptor <DescriptorType>(IPageTemplate templateDefinition,
                                                                                  Func <DescriptorType> descriptorConstructor,
                                                                                  out IDictionary <string, PropertyInfo> placeholderProperties)
            where DescriptorType : PageTemplateDescriptor
        {
            Verify.ArgumentNotNull(templateDefinition, "templateDefinition");
            Verify.ArgumentNotNull(descriptorConstructor, "descriptorConstructor");

            DescriptorType pageTemplate = descriptorConstructor();

            Verify.ArgumentCondition(pageTemplate != null, "descriptorConstructor", "Not null object expected");

            if (templateDefinition.TemplateId == null || templateDefinition.TemplateId == Guid.Empty)
            {
                throw new InvalidOperationException("TemplateId has not been correctly defined");
            }

            pageTemplate.Id    = templateDefinition.TemplateId;
            pageTemplate.Title = templateDefinition.TemplateTitle;

            string defaultPlaceholderId = null;

            var placeholders = new List <PlaceholderDescriptor>();

            placeholderProperties = new Dictionary <string, PropertyInfo>();

            var type = templateDefinition.GetType();

            while (type.GetInterfaces().Contains(typeof(IPageTemplate)))
            {
                foreach (var property in type.GetProperties())
                {
                    if (property.ReflectedType != property.DeclaringType)
                    {
                        continue;
                    }

                    var placeholderAttributes = property.GetCustomAttributes(typeof(PlaceholderAttribute), true);
                    if (placeholderAttributes.Length == 0)
                    {
                        continue;
                    }

                    Verify.That(placeholderAttributes.Length == 1, "Multiple '{0}' attributes defined on property", typeof(PlaceholderAttribute), property.Name);

                    var placeholderAttribute = (PlaceholderAttribute)placeholderAttributes[0];

                    string placeholderId    = placeholderAttribute.Id ?? property.Name;
                    string placeholderLabel = placeholderAttribute.Title ?? property.Name;

                    if (placeholderProperties.ContainsKey(placeholderId))
                    {
                        throw new InvalidOperationException("Placeholder '{0}' defined multiple times".FormatWith(placeholderId));
                    }

                    var containerClasses = ContainerClassManager.ParseToList(placeholderAttribute.ContainerClasses).ToList();

                    placeholderProperties.Add(placeholderId, property);
                    placeholders.Add(new PlaceholderDescriptor {
                        Id = placeholderId, Title = placeholderLabel, ContainerClasses = containerClasses
                    });


                    if (placeholderAttribute.IsDefault)
                    {
                        Verify.IsNull(defaultPlaceholderId, "More than one placeholder is marked as default");

                        defaultPlaceholderId = placeholderId;
                    }
                }

                type = type.BaseType;
            }

            if (defaultPlaceholderId == null && placeholders.Any())
            {
                defaultPlaceholderId = placeholders.First().Id;
            }

            pageTemplate.DefaultPlaceholderId    = defaultPlaceholderId;
            pageTemplate.PlaceholderDescriptions = placeholders;

            return(pageTemplate);
        }
Esempio n. 13
0
 public PageTemplate(IPageTemplate template)
 {
     this.template = template;
     this.metadataFields = new FieldSet(template.MetadataFields);
 }
        /// <summary>
        /// Creates predefined Regions from Page Template metadata.
        /// </summary>
        private static void CreatePredefinedRegions(RegionModelSet regions, IPageTemplate pageTemplate)
        {
            IFieldSet ptMetadataFields = pageTemplate.MetadataFields;
            IField regionsField;
            if (ptMetadataFields == null || !ptMetadataFields.TryGetValue("regions", out regionsField)) // TODO: "region" instead of "regions"
            {
                Log.Debug("No Region metadata defined for Page Template '{0}'.", pageTemplate.Id);
                return;
            }

            foreach (IFieldSet regionMetadataFields in regionsField.EmbeddedValues)
            {
                IField regionViewNameField;
                if (!regionMetadataFields.TryGetValue("view", out regionViewNameField))
                {
                    Log.Warn("Region metadata without 'view' field encountered in metadata of Page Template '{0}'.", pageTemplate.Id);
                    continue;
                }

                MvcData regionMvcData = new MvcData(regionViewNameField.Value);
                InitializeRegionMvcData(regionMvcData);
                RegionModel regionModel = CreateRegionModel(regionMvcData);
                regions.Add(regionModel);
            }
        }