Esempio n. 1
0
        public void Transform(Engine engine, Package package)
        {
            if (package.GetByName(Package.PageName) == null)
            {
                return;
            }
            comm.Page page = (comm.Page)engine.GetObject(package.GetByName(Package.PageName));
            Dictionary <string, List <ComponentPresentation> > lists = new Dictionary <string, List <ComponentPresentation> >();
            string matchTokens = package.GetValue("ComponentTemplateTitleMatchTokens");

            if (!String.IsNullOrEmpty(matchTokens))
            {
                foreach (string token in matchTokens.Split(','))
                {
                    _matchTokens.Add(token.Trim());
                }
            }

            foreach (comm.ComponentPresentation cp in page.ComponentPresentations)
            {
                string ct = GetPresentationType(cp.ComponentTemplate.Title);
                if (!lists.ContainsKey(ct))
                {
                    lists.Add(ct, new List <ComponentPresentation>());
                }
                lists[ct].Add(new ComponentPresentation(cp.Component.Id, cp.ComponentTemplate.Id));
            }

            foreach (string token in lists.Keys)
            {
                Item item = package.CreateStringItem(ContentType.ComponentArray, ComponentPresentationList.ToXml(lists[token]));
                package.PushItem(token + "Components", item);
            }
        }
 /// <summary>
 /// Add a new <see cref="T:Tridion.ContentManager.Templating.ComponentPresentationList"/> value to the current <see cref="T:Tridion.ContentManager.Templating.Package" />
 /// </summary>
 /// <param name="package"><see cref="T:Tridion.ContentManager.Templating.Package" /></param>
 /// <param name="name">Name.</param>
 /// <param name="value">Value.</param>
 public static void AddComponents(this Package package, String name, ComponentPresentationList value)
 {
     if (package != null)
     {
         Item packageItem = package.CreateStringItem(ContentType.ComponentArray, value != null ? value.ToXml() : new ComponentPresentationList().ToXml());
         package.PushItem(name, packageItem);
     }
 }
Esempio n. 3
0
 /// <summary>
 /// Add a new <see cref="T:Tridion.ContentManager.Templating.ComponentPresentationList"/> value to the current <see cref="T:Tridion.ContentManager.Templating.Package" />
 /// </summary>
 /// <param name="package"><see cref="T:Tridion.ContentManager.Templating.Package" /></param>
 /// <param name="name">Name.</param>
 /// <param name="value">Value.</param>
 public static void AddComponents(this Package package, String name, ComponentPresentationList value)
 {
     if (package != null)
     {
         Item packageItem = package.CreateStringItem(ContentType.ComponentArray, value != null ? value.ToXml() : new ComponentPresentationList().ToXml());
         package.PushItem(name, packageItem);
     }
 }
        protected void partitionCPs(IList <PartitionDescriptor> partitionDescriptors)
        {
            var pageItem = _package.GetByType(ContentType.Page);

            if (pageItem == null)
            {
                throw new WrongKindOfRenderException();
            }
            var    pageAsSource = pageItem.GetAsSource();
            TcmUri pageUri      = new TcmUri(pageAsSource.GetValue("ID"));
            Page   page         = (Page)_engine.GetSession().GetObject(pageUri);

            // Start by setting up the list of Component Presentations that we are going to populate
            var cpLists = new Dictionary <string, ComponentPresentationList>();

            foreach (var pd in partitionDescriptors)
            {
                cpLists.Add(pd.name, new ComponentPresentationList());
            }

            // Finally add one called Components, for all the ones not matched by a partition descriptor
            try
            {
                cpLists.Add(Package.ComponentsName, new ComponentPresentationList());
            }
            catch (ArgumentException ex)
            {
                // There may be other reasons why you'd get an argument exception here. Production code would deal with this...
                throw new ComponentsNameIsReservedException("Your own component presentation lists may not be called Components.", ex);
            }


            // OK - now we loop through the Component Presentations in the Page...
            foreach (var cp in page.ComponentPresentations)
            {
                Tridion.ContentManager.Templating.ComponentPresentation templatingCP =
                    new Tridion.ContentManager.Templating.ComponentPresentation(
                        new TcmUri(cp.Component.Id), new TcmUri(cp.ComponentTemplate.Id));

                // ... and if they match one of our partitions, add it to the appropriate list
                bool matched = false;
                foreach (var pd in partitionDescriptors)
                {
                    if (pd.test(cp.Component, cp.ComponentTemplate))
                    {
                        cpLists[pd.name].Add(templatingCP);
                        matched = true;
                        break;
                    }
                }
                // ... if not, put it into "Components"
                if (!matched)
                {
                    cpLists[Package.ComponentsName].Add(templatingCP);
                }

                // We want the components in the package as well as the collections, for compatibility with ExtractComponentsFromPage
                Item componentItem = _package.CreateTridionItem(ContentType.Component, new TcmUri(cp.Component.Id));
                _package.PushItem(Package.ComponentName, componentItem);
            }

            // Add each collection to the package, and we're done.
            foreach (var list in cpLists)
            {
                ComponentPresentationList cpl = (ComponentPresentationList)list.Value;
                Item componentArray           = _package.CreateStringItem(
                    ContentType.ComponentArray,
                    cpl.ToXml());
                _package.PushItem(list.Key, componentArray);
            }
        }