public static Dynamic.ComponentPresentation BuildComponentPresentation(TCM.ComponentPresentation tcmComponentPresentation, Engine engine, BuildManager manager)
        {
            TemplatingLogger logger = TemplatingLogger.GetLogger(typeof(ComponentPresentationBuilder));

            Dynamic.ComponentPresentation cp = new Dynamic.ComponentPresentation();

            logger.Debug(string.Format(">BuildCP {0} ({1})", tcmComponentPresentation.ComponentTemplate.Title, tcmComponentPresentation.ComponentTemplate.IsRepositoryPublishable));
            if (tcmComponentPresentation.ComponentTemplate.IsRepositoryPublishable)
            {
                // call render but ignore the output - render ensures componentlinking will be setup as normal.
                // don't bother with page flags because the end result is dynamically published so it needs to run with DCP settings
                engine.RenderComponentPresentation(tcmComponentPresentation.Component.Id, tcmComponentPresentation.ComponentTemplate.Id);

                // ignore the rendered CP, because it is already available in the broker
                // instead, we will render a very simple version without any links
                cp.Component = manager.BuildComponent(tcmComponentPresentation.Component, 0); // linkLevel = 0 means: only summarize the component
                cp.IsDynamic = true;
            }
            else
            {
                // render the component presentation using its own CT
                // but first, set a parameter in the context so that the CT will know it is beng called
                // from a DynamicDelivery page template
                if (engine.PublishingContext.RenderContext != null && !engine.PublishingContext.RenderContext.ContextVariables.Contains(BasePageTemplate.VariableNameCalledFromDynamicDelivery))
                {
                    engine.PublishingContext.RenderContext.ContextVariables.Add(BasePageTemplate.VariableNameCalledFromDynamicDelivery, BasePageTemplate.VariableValueCalledFromDynamicDelivery);
                }

                string renderedContent = engine.RenderComponentPresentation(tcmComponentPresentation.Component.Id, tcmComponentPresentation.ComponentTemplate.Id);
                renderedContent = TridionUtils.StripTcdlTags(renderedContent);

                // rendered content could contain si4t search data. if that's the case, the value of renderedCotnent is not valid DD4T data.
                // lets remove the si4t search data if that's the case.
                string dd4tData = Si4tUtils.RemoveSearchData(renderedContent);

                try
                {
                    // we cannot be sure the component template uses the same serializer service as the page template
                    // so we will call a factory which can detect the correct service based on the content
                    ISerializerService serializerService = SerializerServiceFactory.FindSerializerServiceForContent(dd4tData);
                    cp = serializerService.Deserialize <Dynamic.ComponentPresentation>(dd4tData);

                    // inital renderedContent could contain si4t search data. we need to preserve the search data.
                    // lets retrieve the si4t search data if that's the case and added to the renderedContent property
                    cp.RenderedContent = Si4tUtils.RetrieveSearchData(renderedContent);
                }
                catch (Exception e)
                {
                    log.Error("exception while deserializing into CP", e);
                    // the component presentation could not be deserialized, this probably not a Dynamic Delivery template
                    // just store the output as 'RenderedContent' on the CP
                    cp.RenderedContent = renderedContent;
                    // because the CT was not a DD4T CT, we will generate the DD4T XML code here
                    cp.Component = manager.BuildComponent(tcmComponentPresentation.Component);
                }
                cp.IsDynamic = false;
            }
            cp.ComponentTemplate = manager.BuildComponentTemplate(tcmComponentPresentation.ComponentTemplate);
            return(cp);
        }
Example #2
0
        /// <summary>
        /// Creates an Entity Data Model from a given CM Component Presentation on a Page.
        /// </summary>
        /// <param name="cp">The CM Component Presentation (obtained from a Page).</param>
        public EntityModelData CreateEntityModel(ComponentPresentation cp)
        {
            EntityModelData entityModelData = null;

            foreach (IEntityModelDataBuilder entityModelBuilder in _entityModelBuilders)
            {
                entityModelBuilder.BuildEntityModel(ref entityModelData, cp);
            }
            return(entityModelData);
        }
        public static Dynamic.ComponentPresentation BuildComponentPresentation(TCM.ComponentPresentation tcmComponentPresentation, Engine engine, int linkLevels, bool resolveWidthAndHeight, BuildManager manager)
        {
            Dynamic.ComponentPresentation cp = new Dynamic.ComponentPresentation();

            if (tcmComponentPresentation.ComponentTemplate.IsRepositoryPublishable)
            {
                // call render but ignore the output - render ensures componentlinking will be setup as normal.
                // don't bother with page flags because the end result is dynamically published so it needs to run with DCP settings
                engine.RenderComponentPresentation(tcmComponentPresentation.Component.Id, tcmComponentPresentation.ComponentTemplate.Id);

                // ignore the rendered CP, because it is already available in the broker
                // instead, we will render a very simple version without any links
                cp.Component = manager.BuildComponent(tcmComponentPresentation.Component, 0, false, false); // linkLevels = 0 means: only summarize the component
                cp.IsDynamic = true;
            }
            else
            {
                // render the component presentation using its own CT
                // but first, set a parameter in the context so that the CT will know it is beng called
                // from a DynamicDelivery page template
                if (engine.PublishingContext.RenderContext != null && !engine.PublishingContext.RenderContext.ContextVariables.Contains(BasePageTemplate.VariableNameCalledFromDynamicDelivery))
                {
                    engine.PublishingContext.RenderContext.ContextVariables.Add(BasePageTemplate.VariableNameCalledFromDynamicDelivery, BasePageTemplate.VariableValueCalledFromDynamicDelivery);
                }

                string renderedContent = engine.RenderComponentPresentation(tcmComponentPresentation.Component.Id, tcmComponentPresentation.ComponentTemplate.Id);
                engine.PublishingContext.RenderContext.ContextVariables.Remove(BasePageTemplate.VariableNameCalledFromDynamicDelivery);

                renderedContent = TridionUtils.StripTcdlTags(renderedContent);

                cp.IsDynamic = false;
                TextReader tr = new StringReader(renderedContent);
                if (serializer == null)
                {
                    serializer = new ComponentSerializer();
                    //serializer = new XmlSerializerFactory().CreateSerializer(typeof(Dynamic.Component));
                }
                try
                {
                    cp.Component = (Dynamic.Component)serializer.Deserialize(tr);
                }
                catch (Exception e)
                {
                    TemplatingLogger.GetLogger(typeof(ComponentPresentationBuilder)).Error("exception while deserializing into CP: " + e.Message);
                    // the component presentation could not be deserialized, this probably not a Dynamic Delivery template
                    // just store the output as 'RenderedContent' on the CP
                    cp.RenderedContent = renderedContent;
                    // because the CT was not a DD4T CT, we will generate the DD4T XML code here
                    cp.Component = manager.BuildComponent(tcmComponentPresentation.Component);
                }
            }
            cp.ComponentTemplate = manager.BuildComponentTemplate(tcmComponentPresentation.ComponentTemplate);
            return(cp);
        }
Example #4
0
        public static Dynamic.ComponentPresentation BuildComponentPresentation(TCM.ComponentPresentation tcmComponentPresentation, Engine engine, int linkLevels, bool resolveWidthAndHeight, BuildManager manager)
        {
            Dynamic.ComponentPresentation cp = new Dynamic.ComponentPresentation();


            // render the component presentation using its own CT
            // but first, set a parameter in the context so that the CT will know it is beng called
            // from a DynamicDelivery page template
            if (engine.PublishingContext.RenderContext != null && !engine.PublishingContext.RenderContext.ContextVariables.Contains(PageTemplateBase.VariableNameCalledFromDynamicDelivery))
            {
                engine.PublishingContext.RenderContext.ContextVariables.Add(PageTemplateBase.VariableNameCalledFromDynamicDelivery, PageTemplateBase.VariableValueCalledFromDynamicDelivery);
            }

            string renderedContent = engine.RenderComponentPresentation(tcmComponentPresentation.Component.Id, tcmComponentPresentation.ComponentTemplate.Id);

            engine.PublishingContext.RenderContext.ContextVariables.Remove(PageTemplateBase.VariableNameCalledFromDynamicDelivery);

            renderedContent = TridionUtils.StripTcdlTags(renderedContent);

            if (tcmComponentPresentation.ComponentTemplate.IsRepositoryPublishable)
            {
                // ignore the rendered CP, because it is already available in the broker
                // instead, we will render a very simple version without any links
                cp.Component = manager.BuildComponent(tcmComponentPresentation.Component, 0, false); // linkLevels = 0 means: only summarize the component
            }
            else
            {
                TextReader tr = new StringReader(renderedContent);
                if (serializer == null)
                {
                    serializer = new XmlSerializerFactory().CreateSerializer(typeof(Dynamic.Component));
                }
                try
                {
                    cp.Component = (Dynamic.Component)serializer.Deserialize(tr);
                }
                catch
                {
                    // the component presentation could not be deserialized, this probably not a Dynamic Delivery template
                    // just store the output as 'RenderedContent' on the CP
                    cp.RenderedContent = renderedContent;
                }
            }
            cp.ComponentTemplate = manager.BuildComponentTemplate(tcmComponentPresentation.ComponentTemplate);
            return(cp);
        }
Example #5
0
 public virtual Dynamic.ComponentPresentation BuildComponentPresentation(TComm.ComponentPresentation tcmComponentPresentation, Engine engine, int linkLevels, bool resolveWidthAndHeight)
 {
     return(ComponentPresentationBuilder.BuildComponentPresentation(tcmComponentPresentation, engine, this));
 }
        public void Transform(Engine engine, Package package)
        {
            // Find the best fit for SEO Keywords and Description for this page's content.
            // Leave if we're not rendering a page
            if (package.GetByName(Package.PageName) == null)
            {
                return;
            }

            List <string> seoKeywords    = new List <string>();
            string        seoDescription = string.Empty;

            Page page = (Page)engine.GetObject(package.GetByName(Package.PageName));

            if (page.Metadata != null)
            {
                ItemFields metadata = new ItemFields(page.Metadata, page.MetadataSchema);
                foreach (ItemField field in metadata)
                {
                    if (field.Name.Equals("ContentSource"))
                    {
                        seoKeywords.Add(((KeywordField)field).Value.Title);
                    }
                    if (field.Name.Equals("Keywords"))
                    {
                        TextField mvKeywords = (TextField)field;
                        foreach (string keyword in mvKeywords.Values)
                        {
                            seoKeywords.Add(keyword);
                        }
                    }
                    if (field.Name.Equals("Description"))
                    {
                        TextField description = (TextField)field;
                        seoDescription = description.Value;
                    }
                }
            }
            // Check if we have data...
            if (string.IsNullOrEmpty(seoDescription))
            {
                // Get description from first component.
                // If it exists...
                // If page has no components, then it must be a dynamic index page, we should get the description to match the page title
                if (page.ComponentPresentations.Count.Equals(0))
                {
                    seoDescription = package.GetValue("ArticlesByText");
                    // and the first seoKeyword should be the Content Source field.
                    if (seoKeywords.Count >= 1)
                    {
                        seoDescription += " " + seoKeywords[0];
                    }
                }
                else
                {
                    // Let's look at the content
                    // First find the type of page.
                    if (page.PageTemplate.Title.Contains("Content") || page.PageTemplate.Title.Contains("Home"))
                    {
                        // This is either a one page article, or the home page. In both cases, the SEO Description is the ArticleTitle of the first component
                        CP        cp = page.ComponentPresentations[0];
                        Component c  = cp.Component;
                        if (c.Schema.Title.Equals("Article"))
                        {
                            ItemFields content = new ItemFields(c.Content, c.Schema);
                            TextField  title   = (TextField)content["ArticleTitle"];
                            seoDescription = title.Value;
                            // If we have no keywords and the it's a content page, let's add the author to the keywords
                            if (seoKeywords.Count == 0 && page.PageTemplate.Title.Contains("Content"))
                            {
                                KeywordField source = (KeywordField)content["Source"];
                                if (source.Values.Count > 0)
                                {
                                    seoKeywords.Add(source.Value.Title);
                                }
                            }
                        }
                    }
                }
            }
            if (!(string.IsNullOrEmpty(seoDescription)))
            {
                package.PushItem(SeoDescriptionName, package.CreateStringItem(ContentType.Text, seoDescription));
            }
            if (seoKeywords.Count <= 0)
            {
                return;
            }
            string keywords = string.Empty;

            for (int i = 0; i < seoKeywords.Count; i++)
            {
                if (i > 0)
                {
                    keywords += ", ";
                }
                keywords += seoKeywords[i];
            }
            package.PushItem(SeoKeywordsName, package.CreateStringItem(ContentType.Text, keywords));
        }