Developer forum

Forum » Development » Custom date tags not rendering in NotificationSubscriber

Custom date tags not rendering in NotificationSubscriber

Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Hi there,

It seems that on DW 8.4 global tags with a date value are not rendered. I have the following custom code:

[Dynamicweb.Extensibility.Subscribe(Standard.Page.OnGlobalTags)]
public class PageOnGlobalTagsObserver1 : Dynamicweb.Extensibility.NotificationSubscriber
{
  public override void OnNotify(string notification, Dynamicweb.Extensibility.NotificationArgs args)
  {
    var pageviewNotificationArgs = args as Standard.Page.PageviewNotificationArgs;
    pageviewNotificationArgs.Template.SetTag("Test", "This works");
    pageviewNotificationArgs.Template.SetTag("Yesterday", DateTime.Now.AddDays(-1)); // This doesn't work
  }
}

When I run this code, my Test tag with a value "This works" shows up in DwTemplateTags and I can render its value using @Test. However, the tag Yesterday and all of its child tags for date values do not show up.

Is this expected behavior?

Thanks,

Imar

 


Replies

 
Nicolai Høeg Pedersen
Reply

If your template instance setting the tag is a Razor template, all the extensions of dates are not set - only a date object is set.

You can call GetDate("Yesterday") and get the date object in the template and use .NET date operations on the tag.

It should not be a 8.4 behavior - it has been like this since May. The tag does not show up in DwTemplateTags.

But this is global tags... So the template instance does not have a file, and cannot be Razor. Just checking code base....

It seems like the template instance is now Razor by default if no template file is specified - that is changed behavior and a bug. You can do like this and then it should work. A fix is out in 8.4.0.7 due next Tuesday. TFS#14420

var pageviewNotificationArgs = args as Standard.Page.PageviewNotificationArgs;
pageviewNotificationArgs.Template.Type = TemplateType.Html
pageviewNotificationArgs.Template.SetTag("Test", "This works");
pageviewNotificationArgs.Template.SetTag("Yesterday", DateTime.Now.AddDays(-1));

BR Nicolai

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

It turns out this is still an issue. I upgraded to 8.4.1.2 and I am using  a plain HTML layout file. I then have the following extension code;

[Dynamicweb.Extensibility.Subscribe(Standard.Page.OnGlobalTags)]
public class PageOnGlobalTagsObserver1 : Dynamicweb.Extensibility.NotificationSubscriber
{
  public override void OnNotify(string notification, Dynamicweb.Extensibility.NotificationArgs args)
  {
    var pageviewNotificationArgs = args as Standard.Page.PageviewNotificationArgs;
    pageviewNotificationArgs.Template.Type = Template.TemplateType.Html;
    pageviewNotificationArgs.Template.SetTag("Yesterday", DateTime.Now.AddDays(-1)); // This doesn't work because it's a date
    pageviewNotificationArgs.Template.SetTag("Tomorrow", DateTime.Now.AddDays(1).ToString()); // This works because it's a string
  }
}

When I run this with these tags:

Yesterday: <!--@Yesterday--><br />
Tomorrow: <!--@Tomorrow--><br />

I get this output:

Yesterday:
Tomorrow: 5/14/2014 12:58:34 PM

Notice how the Tomorrow tag works because it's now a string, and not a date.

Is this a bug? Or is there a reason why dates don't work globally?

Imar

 
Morten Bengtson
Reply
This post has been marked as an answer

When the template type is TemplateType.Html then Template.SetTag(string, DateTime) checks if the template tag exist in the template HTML and only adds the date values if the tag exist. The global tags are set on a temporary template object which has no HTML, so the template tag does not exist and the date values are not added.

As a workaround, you could try to prefix the name of your global template tag with "Global:", e.g. "Global:Yesterday"... or use Razor instead :)

Votes for this answer: 1
 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Hi Morten,

Thanks for that; using the Global: prefix did the trick. Do you know why that is?

Imar

 
Mikkel Ricky
Reply

For performance reasons Date tags are only set when the underlying template actually uses the tags, i.e. it contains the tag name, or if the tag name starts with "Global:". The global template is empty and therefore it does not use any tags.

Changing this behaviour will result in major performance issues and therefore we cannot change it. Furthermore, it makes sense that a global tag's name starts with "Global:".

Mortens suggestion is the way to do it, i.e. use Razor.

Best regards,
Mikkel

 
Imar Spaanjaars Dynamicweb Employee
Imar Spaanjaars
Reply

Hi Mikkel,

Perfect, thanks. No change is required as the work arounds are more than enough.

Imar

 

You must be logged in to post in the forum