Developer forum

Forum » Development » Global tags not working for loops?

Global tags not working for loops?

Jon Lennryd
Reply
Hi!
I register some global tags like this; template.SetTag("Global:NiftyTag", "stuff");
which is working fine.

Now I tried to create a loop, like this:
            foreach (Group group in groups)
            {
                this.DebugMessage("Name: " + group.Name);

                Dynamicweb.Templatev2.Template loopTemplate = template.GetLoop("Global:ProductCategories");

                loopTemplate.SetTag("GroupName", group.Name);
                loopTemplate.SetTag("GroupSmallImage", group.SmallImage);

                loopTemplate.SetTag("GroupLink", "Default.aspx?ID=" + pageID + "&GroupID=" + group.ID);

                loopTemplate.CommitLoop();
            }
But this is not working. In the template file, I have the following code:

    <!--@LoopStart(Global:ProductCategories)-->
        <a href="<!--@GroupLink-->"><!--@GroupName--></a>
    <!--@LoopEnd(Global:ProductCategories)-->

Which is producing the following output, indicating the loop is undefined:
    <!--@LoopOutput(Global:ProductCategories_0)-->


How can I get this working? 







Replies

 
Morten Snedker
Reply
 Try inserting the 

<!--@DwTemplateTags-->

tag into you template to verify that your tags are actually available. In which context is you code executed (PageLoad, ContentModule...)?
 
Jon Lennryd
Reply
Yup. My tags are available, except the loop tag. I define the loop tag and the other tags as Global:blaha.

I use the Dynamicweb.Notifications.Standard.Page.OnGlobalTags event to set this tags on every page load.
 
Jon Lennryd
Reply
I paste my actual code below, with comments added:
    [Subscribe(Dynamicweb.Notifications.Standard.Page.OnGlobalTags)]
    public class CleenleepPageLoadedEventHandler : Dynamicweb.Extensibility.NotificationSubscriber
    {
        public override void OnNotify(string notification, Dynamicweb.Extensibility.NotificationArgs args)
        {
            Dynamicweb.Notifications.Standard.Page.PageviewNotificationArgs s = (Dynamicweb.Notifications.Standard.Page.PageviewNotificationArgs)args;

            // This will work and set a global tag:
            s.Template.SetTag("FantasticTag", "CoolContent");

            // Get a list of groups. (returns a couple of eCommerce groups)
            List<Group> groups = this.GetCategories();

            // This code won't work: (the tag is not defined when I try to use it in the template.)
            foreach (Group group in groups)
            {
                Dynamicweb.Templatev2.Template loopTemplate = template.GetLoop("Global:ProductCategories");
                loopTemplate.SetTag("GroupName", group.Name);
                loopTemplate.CommitLoop();
            }
        }
    }

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply
I think Global could be a reserved word. Did you try a different prefix?

Imar
 
Jon Lennryd
Reply
I have tried different naming. The Global prefix should be there according to docs.
 
Nicolai Høeg Pedersen
Reply
This post has been marked as an answer
Hi Jon.

Just to be sure:
Are you on the Notifications.Standard.Page.OnGlobalTags trying to add a loop?
Because that is not possible.

If you want a "Global" loop, try the same using the Notifications.Standard.Page.AfterOutput which works very differently in this regard.

Votes for this answer: 0

 

You must be logged in to post in the forum