Developer forum

Forum » Development » Setting PageTitle on product page with Notification Subscriber

Setting PageTitle on product page with Notification Subscriber

Vincent Gercke
Reply

I am having difficulties setting the page title with the PageTitle Notification Subscriber when viewing individual product pages. It seems to work as intended on other pages, but it looks like the title gets overwritten on product pages. Has anyone else experienced this before? :-) The code can be seen below.

public override void OnNotify(string notification, NotificationArgs args)
        {
            if (args == null)
                return;

            Dynamicweb.Notifications.Standard.Page.PageTitleArgs pta = (Dynamicweb.Notifications.Standard.Page.PageTitleArgs)args;

            var pageTitleText = pta.PageView.Area.Item["PageTitleText"];

            if (pageTitleText != null)
            {
                pta.PageView.Meta.Title += $" - {pageTitleText}";
            }
        }


Replies

 
Nicolai Pedersen
Reply
This post has been marked as an answer

Yes, that is because the view product does the same as you (more or less) and override your title.

If you are using "regular" templates with template tags, either html or Razor, you can createa a template extender instead:

public class PageTemplateExtenderSample : PageTemplateExtender
    {
        public override void ExtendTemplate(Dynamicweb.Rendering.Template template)
        {
            template.SetTag("Title", PageView.Meta.Title + " | SEO Experts invent something new to keep in business.");
        }
    }

If you use ViewModel based layout template, simply change the title tag - it cannot be done with extenders or subscribers:

@{
    var pageTitleText = pta.PageView.Area.Item["PageTitleText"];
    if (pageTitleText != null)
    {
        pageTitleText += $" - {pageTitleText}";
    }
}
<title>@Model.Title@pageTitleText</title>

BR Nicolai

Votes for this answer: 1
 
Vincent Gercke
Reply

Great! Thanks for the quick response.

 

You must be logged in to post in the forum