Developer forum

Forum » Development » How to make a global loop in a Standard.Page.OnGlobalTags subscriber

How to make a global loop in a Standard.Page.OnGlobalTags subscriber

Jesper Laustsen
Reply

Sorry for handling this issue by re-posting, but as it is a rather old post, I am now reposting the matter:

http://developer.dynamicweb-cms.com/forum/development/how-to-make-a-global-loop-in-a-standard.page.onglobaltags-subscriber-1.aspx

The issue here is, that even though I use the suggested approach, my global:loop does not show in DwTemplateTags, nor does it render inside the content module, where the global tags are needed.

This could be a bug or it could be that the appoeach is either insufficient or entirely wrong. Please help out if you can.

My code:

public class NotificationOnGlobalTagsFooterBrands : NotificationSubscriber
    {
        public override void OnNotify(string notification, Dynamicweb.Extensibility.NotificationArgs args)
        {
            Dynamicweb.Notifications.Standard.Page.PageviewNotificationArgs a = (Dynamicweb.Notifications.Standard.Page.PageviewNotificationArgs)args;

            MrBrand BrandClass = new MrBrand();
            List<MrBrand> Brands = BrandClass.GetBrands();
            a.Template.SetTag("Global:NIQ:Test", "Global test");

            if (a.Template.LoopExists("Global:NIQ:Brandsloop"))
            {
                Dynamicweb.Templatev2.Template BrandsLoop = a.Template.GetLoop("Global:NIQ:BrandsLoop");
                foreach (MrBrand b in Brands)
                {
                    BrandsLoop.SetTag("Global:NIQ:BrandsLoop.Brand", b.Brand);
                    BrandsLoop.SetTag("Global:NIQ:BrandsLoop.Description", b.Description);
                    BrandsLoop.SetTag("Global:NIQ:BrandsLoop.Sort", b.Sort);
                    BrandsLoop.SetTag("Global:NIQ:BrandsLoop.Classification", b.Classification);
                    BrandsLoop.SetTag("Global:NIQ:BrandsLoop.ShowInFooter", b.Showinfooter);
                    BrandsLoop.SetTag("Global:NIQ:BrandsLoop.Logolink", b.Logolink);

                    BrandsLoop.CommitLoop();
                }
            }
        }
    }

Replies

 
Nicolai Høeg Pedersen
Reply
Global template tags works a bit differently than other template instances.

The global template tags object you get from the notification is an "empty placeholder" template instance. Then whenever a real template instance is created, it loads the collection of tags from the Global template placeholder instance.

Because of this, loops will not be able to be applied in global tags using the OnGlobalTags notification.

What you can do is changing to Notifications.Standard.Page.AfterOutput notification - the arguments to that notification holds a template instance with the entire rendered page, and on that you can apply your loop.

 [Extensibility.Subscribe(Dynamicweb.Notifications.Standard.Page.AfterOutput)]
    public class PageAfterOutputObserver : Dynamicweb.Extensibility.NotificationSubscriber
    {
        public override void OnNotify(string notification, Dynamicweb.Extensibility.NotificationArgs args)
        {
            if (args == null)            
                return;

            Dynamicweb.Notifications.Standard.Page.AfterOutputArgs afterOutputArgs = (Dynamicweb.Notifications.Standard.Page.AfterOutputArgs)args;
            afterOutputArgs.template.SetTag("myTag", "some text");
        }
    }


 

You must be logged in to post in the forum