Developer forum

Forum » Development » Set true global tag

Set true global tag


Reply
Hi,

Need a fast rebound on this one:

I need to set a global tag, that enables this international shop to disallow shopping, if users country is not matched. The customer allows shopping for e.g. Scandinavia, so if you are from Germany, the cart, add to cart buttons and so on are disabled.

Tags set on a page template extender can be rendered on a paragraph template, product template, ordertemplate, but it is not possible to ask "If Defined" on them.

The solution, as I read else where on the forum was to make a notification subscriber on

Dynamicweb.Notifications.Standard.Page.AfterOutput


... but this works exactly the same way as tags from a template extender. Tags are rendered, but can not be parsed through "If Defined".

Is there by any chance a solution to this? And especially how to extend a paragraph to see a global custom tag?

Regards,

Jesper

Replies

 
Reply
 as far as i understand the "If Defined" then it checks for something or nothing.

So if you in the case of False, passes en empty string, then "If Defined"  will pick it up.

setting the the tags something like this should work. 

if True
    myTestTag = "true"

if false
    myTestTag = ""

/kenneth
 
Reply
Try using the notification Dynamicweb.Notifications.Standard.Page.OnOutput which is called before if defined is parsed...

1. Notify OnOutput
2. Parse If Defined
3. Set standard global tags
4. Notify AfterOutput

True global tags will be available in 7.2 with a new notification (OnGlobalTags), but OnOutput should do just fine in this case.

Also, since version 19.1.2.1, you can just use the standard Template.SetTag(string Name, bool Value) which will output "true" when true and string.empty when false.

BR.
Morten
 
Reply
Hey Kenneth,

Thanks for your reply,

I am fully aware of how "If Defined" works, which is also why I only publish the tag if the criterion is met, otherwise there is no tag. But this still does not make "If Defined" work.

Jesper
 
Reply

Ok.. better paste my code :-)


[Subscribe(Dynamicweb.Notifications.Standard.Page.AfterOutput)]
    public class NotificationGlobalTags : NotificationSubscriber
    {

        public override void OnNotify(string notification, object[] args)
        {
            if (args.Length > 0)
            {
                try
                {
                    if (HttpContext.Current.Session["SelectedCountry"] != null)
                    {
                        if (HttpContext.Current.Session["SelectedCountry"].ToString() == "int")
                        {
                            Dynamicweb.Templatev2.Template t = (Dynamicweb.Templatev2.Template)args[0];
                            t.SetTag("NIQ:CountrySelector.Global.DisableShopping", true.ToString());
                        }
                    }
                }
                catch (Exception e)
                {
                    Base.wa("Error casting args to type Template: " + e.InnerException);
                }
            }
        }
    }

 

Notifications.Standard.Page.OnOutput  doesnt work either.

 
Reply
and just to make it perfectly clear.

... when I say it doesnt work, I am referring to "If defined" on a template sub structure, e.g. a product template. I still cant ask If Defined on the so-called "global tag" from the subscriber. The tag works, meaning it renders its contents on the product template as expected when the criterion is met, but I just cant ask "If defined" on it.

Sincerely,

Jesper
 
Reply
 
[Subscribe(Dynamicweb.Notifications.Standard.Page.OnOutput)]
    public class NotificationGlobalTags : NotificationSubscriber
    {
 
        public override void OnNotify(string notification, object[] args)
        {
Dynamicweb.Templatev2.Template t = args[0] as Dynamicweb.Templatev2.Template;
if (t == null) return;
var selectedCountry = Base.ChkString(HttpContext.Current.Session["SelectedCountry"]).ToLower();
var isInt = selectedCountry.Equals("int");
t.SetTag("NIQ:CountrySelector.Global.DisableShopping", isInt ? bool.TrueString : string.Empty);
        }
    }
 
Reply
 If Defined checks if the VALUE is defined - not the tag. The tag needs to be available in both cases, but with an empty value when false... just like kenneth wrote.

/Morten

 
Reply
Hey Morten,

Thanks for your reply, but that really doesnt make a difference.

If I make sure (setting browser language) that I will hit "International" setting, which sets "Int", and I know for sure that the tag is set, because I can write the tag VALUE to the browser on a product template, can we then not agree on, that if I wrap the tag in "If Defined", it ought to work too on the product template, and the VALUE of the tag should still be output??

/J
 
Reply
Can you post the template?

 
Reply
 if you want to make a golbal tag, then you just have to use a naming convention
Global tags has to start with "Global:"

fx. "Global:myGlobaltag"

then you can set the tag in any template you want, and it will be parsed by the template engine

/kenneth
 
Reply
This is from a ProductList template:

<!--@LoopStart(Products)-->
 a<!--@NIQ:CountrySelector.Global.DisableShopping-->
 <!--@If Defined(NIQ:CountrySelector.Global.DisableShopping)-->
  b<!--@NIQ:CountrySelector.Global.DisableShopping-->
 <!--@EndIf(NIQ:CountrySelector.Global.DisableShopping)-->
<!--@LoopEnd(Products)-->


aTrue will render, while the "b" and that tag value inside "If Defined" does not.

/J
 
Reply
Oh, its inside a loop :)

Does it work outside the loop?

Have you tried setting the tag in a ProductTemplateExtender?

/Morten
 
Reply
Just tried:

<!--@LoopStart(Products)-->
<!--@LoopEnd(Products)-->

  a<!--@NIQ:CountrySelector.Global.DisableShopping-->
 <!--@If Defined(NIQ:CountrySelector.Global.DisableShopping)-->
  b<!--@NIQ:CountrySelector.Global.DisableShopping-->
 <!--@EndIf(NIQ:CountrySelector.Global.DisableShopping)-->

Behaves exactly the same, outputs:

aTrue
 
Reply
Strange. It should work with the ProductTemplateExtender.

Did you try using the tag prefix "Global:" as Kenneth suggested?

Global:CountrySelector.Global.DisableShopping

/Morten
 
Reply
Holy cr..!

IT WORKED :-)

ok... so if a tag is to be truely global, the tag name MUST begin with "Global:tagname".

Thanks Kenneth and Morten for your patience and energy.

Regards,

Jesper

 

You must be logged in to post in the forum