Forums

Skip Navigation LinksHome / Developer Network / Forums / Sitefinity Older Versions (3.x): Developing with Sitefinity > Modify head content page title

Modify head content page title

  • Posted on Oct 1, 2008 (permalink)

    Hi,

    This is probably covered here somewhere, but I can't find it.  I'd like to add a fixed string to the <title> in the head content in each page.  I can't add the text to the page title field in the page, because then it will show up on menus and other places where I programatically use that title.  So what I want is:

    <head>
        <title> Site Name - {page title}</title>

    where {page title} is the text that would automatically be inserted from the page property.

    I'm thinking there's a way to do this by manipulating the head controls, but I'm not sure what the best approach is.

    Thanks again!
    Mike

    Reply

  • Posted on Oct 1, 2008 (permalink)

    Actually, I realize the menus use the menu label field, but I use the Page Title in several controls, and don't want the Site Name showing up there.  Also from the perspective of SEO, page head titles like the following improve your results:

    <title>{Page Title} - {Section} - {Site Name}</title>

    It seems like this would be standard functionality, so maybe I'm missing something simple.

    Thanks,
    Mike

    Reply

  • Posted on Oct 2, 2008 (permalink)

    Well, this is how I decided to implement it.  It doesn't seem elegant, and to improve it I'd rather not hard-code the site name but I'm trying to minimize the impact:

    private bool isFirstLoad = true;  
    private void Page_Load(object sender, EventArgs e)  
    {  
        if (isFirstLoad && this.Page != null && this.Page.Header != null)  
        {  
            isFirstLoad = false;  
            for (int i = 0; i < this.Page.Header.Controls.Count; i++)  
            {  
                if (this.Page.Header.Controls[i].GetType() == typeof(System.Web.UI.HtmlControls.HtmlTitle))  
                {  
                    HtmlTitle title = (HtmlTitle)this.Page.Header.Controls[i];  
                    title.Text = string.Format("MySiteName - {0}", title.Text);  
                    break;  
                }  
            }  
        }  
    }  
     


    Any idea why the page_load method is called twice?

    Thanks,
    Mike



    Reply

  • Georgi Georgi admin's avatar

    Posted on Oct 3, 2008 (permalink)

    Hi Mike,

    We have attached a code that will generate the titles of every of your pages dynamically. Please have a look at it, we think that it will help you to learn how you could change the Page titles dynamically, without any user control. This way, you will escape from the behavior you have with your code also. The particular example changes the titles in a "breadcrumb" fashion.

    You should simply put it in your Project's App_Code directory and declare it in CmsEntryPoint.aspx which is located in your project folder:

    <%@ Page Inherits="SmartTitleInternalPage"
            
    MasterPageFile="~/Sitefinity/Dummy.master" %> 

    Note that you don't need to change anything in your pages, the code will change the titles for the old as well as for all newly created pages.

    You could change the separator symbol as well as the string that the path starts with. You need to change only the values of PathSeparator and PathStartsWith variables.

    We hope you find this example helpful.


    Regards,
    Georgi
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.

    Reply

  • Posted on Oct 3, 2008 (permalink)

    Outstanding!  

    UrlHelper.GetProjectName() is just what I was looking for.  I should have realized I could use the SiteMap.CurrentNode() Title to get around the problem of the page_load getting called twice, but pulling the project name which is set in the web.config is perfect for my needs.

    You folks have the BEST support there is. 

    One more question, though.  I'm not sure I'm following you about CmsEntryPoint.aspx.  I've seen this file, and dummy.master, but I don't really know what they're for.  Is this a sort of "super master" that you can put global overrides in?   In other words, I put my code there, and don't have to put it in each of my other master pages?

    Once again, thanks for your help!
    Mike

    Reply

  • Posted on Oct 3, 2008 (permalink)

    I guess I'll answer my own question...CmsEntryPoint.aspx does seem to be a way to do global overrides.  I never knew about that, and boy is it going to be handy!  I'm really starting to like Sitefinity...I've already got three more projects in my queue that will be using it.

    Thanks again for the great support!
    Mike

    Reply

  • Georgi Georgi admin's avatar

    Posted on Oct 6, 2008 (permalink)

    Hi Mike,

    Thank you for all those nice words! We all do it for you!
    Here's the answer to your question.

    Dummy.master is the masterpage of cmsentrypoint.aspx. All requests are first going through this webpage (that's why it is called cmsentrypoint), then to any other page :

    Web Request --> cmsentrypoint (page and masterpage) --> user defined pages and master pages. 

    So your logic is completely correct.

    Let us know if you have any other questions, we will be glad to answer them.

    Kind regards,
    Georgi
    the Telerik team

    Check out Telerik Trainer, the state of the art learning tool for Telerik products.

    Reply

  • Jason avatar

    Posted on Apr 12, 2011 (permalink)

    will the approach outlined here still work in sitefinity 3.7?  what about v4.0?

    Thanks,

    Reply

  • Ivan Dimitrov Ivan Dimitrov admin's avatar

    Posted on Apr 12, 2011 (permalink)

    Hello Jason,

    In Sitefinity 4.0 we use ASP.NET Routing. You can inject your logic using PageRouteHandler. Here is is a sample code that shows something similar

    http://www.sitefinity.com/40/help/developers-guide/how-to-how-to-set-the-theme-of-a-page-at-runtime.html

    Greetings,
    Ivan Dimitrov
    the Telerik team

    Reply

  • Jason avatar

    Posted on Apr 12, 2011 (permalink)

    what about 3.7?  I"ve got it working w/o issue in 3.6 but 3.7 it doesn't appear to be working.

    Reply

  • Ivan Dimitrov Ivan Dimitrov admin's avatar

    Posted on Apr 12, 2011 (permalink)

    Hi ,

    InternalPage class has not be changed in 3.6 and 3.7 editions, so you should be able to replace it.

    Regards,
    Ivan Dimitrov
    the Telerik team

    Reply

  • Jason avatar

    Posted on Apr 12, 2011 (permalink)

    Thanks for the super fast replies!  I had my versions confused....I have the SmartTitleInternalPage running on sf 3.5 and I'm trying to get it to work on 3.6 - 3.7.   Did internalpage class change between 3.5 to 3.6/3.7?

    Reply

  • Ivan Dimitrov Ivan Dimitrov admin's avatar

    Posted on Apr 12, 2011 (permalink)

    Hi Jason,

    The way that we process the request and the usage of InternalPage is not changed since Sitefinity 3.2.

    Greetings,
    Ivan Dimitrov
    the Telerik team

    Reply

  • Jason avatar

    Posted on Apr 12, 2011 (permalink)

    Alright, problem must be on my end :)  thanks again....always get good support in these forums and I do appreciate that.

    Reply

  • Jason avatar

    Posted on Apr 12, 2011 (permalink)

    Utilizing the info & attachment on this post as well as on Page title automatically generated from navigational structure and attempting to deploy on SF v3.7 it seems that it works when the Title field (in Page Properties) is blank but if it is populated, it does not.  In v3.5, the value of the Title field is pulled into the browser title and if the title is blank it uses the Menu Label field value.  Was there a change between 3.5 and 3.7 which would cause this.....any thoughts on how to make this work in 3.7?

    Reply

  • Ivan Dimitrov Ivan Dimitrov admin's avatar

    Posted on Apr 13, 2011 (permalink)

    Hi Jason,

    There is a virtual method SetTitle which you can override.

    Kind regards,
    Ivan Dimitrov
    the Telerik team

    Reply

  • Antoine avatar

    Posted on Jan 9, 2012 (permalink)

    Thank you for the direction regarding modifying the title in Sitefinity 4.  I was able to do it in Sitefinity 4.4 using the following code:

    using System;
    using System.Linq;
    using System.Web.UI;
    using Telerik.Sitefinity.Web;
    using Telerik.Sitefinity.Abstractions;
    using Telerik.Microsoft.Practices.Unity;
     
    namespace SitefinityWebApp
    {
        public class CustomRouteHandler : PageRouteHandler
        {
            public override void ProcessRequiredControls(Page page, PageSiteNode data)
            {
                base.ProcessRequiredControls(page, data);
     
                page.Header.Title += " | My Website";
            }
     
            public static void RegisterType()
            {
                ObjectFactory.Container.RegisterType<PageRouteHandler, CustomRouteHandler>();
            }
        }
    }

    using System;
    using System.Linq;
     
    namespace SitefinityWebApp
    {
        public class Global : System.Web.HttpApplication
        {
            void SystemManager_ApplicationStart(object sender, EventArgs e)
            {
                CustomRouteHandler.RegisterType();
            }
     
            protected void Application_Start(object sender, EventArgs e)
            {
                // Code that runs on application startup
                Telerik.Sitefinity.Services.SystemManager.ApplicationStart += new EventHandler<EventArgs>(SystemManager_ApplicationStart); 
            }
     
            ...
       }
    }


    Reply

  • Posted on Apr 11, 2012 (permalink)

    If you use .masterpages and especially if you use a parent .masterpage that all of your pages use you can also add the following logic to your .masterpage code behind.
    this.Page.Title += System.Configuration.ConfigurationManager.AppSettings["CompanyName"].ToString();

    In the above example, I got "fancy" and I am storing the string I want to add in the web.config.

    Reply

  • Ivan Dimitrov Ivan Dimitrov admin's avatar

    Posted on Apr 12, 2012 (permalink)

    Hello,

    Yes, but this is an overhead and not the correct way to do it. First we set the title and use resources. Then you read the web.config and set the title again and this happens for each request made to your master page/page. Also there is no caching mechanism for this and it has to be implemented further, since overriding the SetTitle is included in out page lifecycle.

    All the best,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

    Reply

  • Nancy avatar

    Posted on Apr 26, 2012 (permalink)

    How do you do this in Sitefinity 5?

    Reply

  • Ivan Dimitrov Ivan Dimitrov admin's avatar

    Posted on Apr 30, 2012 (permalink)

    Hello,

    You can do this in Sitefinity 5.0 overriding BuildHttpHandler of PageRouteHandler and after the page set this.Page.Title = "your title";. Then you need to register the custom handler through ObjectFactory class using RegisterType from IUnityContainer.

    Another option is overriding SetPageDirectives method of SitefinityPageResolver class and setting the title there.

    this.AddPageDirectiveValue(pageAttribs, "Title", "my title");

    Then you should register your custom class again through ObjectFactory.

    Kind regards,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

    Reply

  • Register for webinar
Skip Navigation LinksHome / Developer Network / Forums / Sitefinity Older Versions (3.x): Developing with Sitefinity > Modify head content page title