+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Modify head content page title

Modify head content page title

21 posts, 0 answered
  1. Mike
    Mike avatar
    208 posts
    Registered:
    10 Dec 2007
    02 Oct 2008
    Link to this post
    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
  2. Mike
    Mike avatar
    208 posts
    Registered:
    10 Dec 2007
    02 Oct 2008
    Link to this post
    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
  3. Mike
    Mike avatar
    208 posts
    Registered:
    10 Dec 2007
    02 Oct 2008
    Link to this post

    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



  4. Georgi
    Georgi avatar
    3583 posts
    Registered:
    28 Oct 2016
    03 Oct 2008
    Link to this post
    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.
  5. Mike
    Mike avatar
    208 posts
    Registered:
    10 Dec 2007
    03 Oct 2008
    Link to this post
    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
  6. Mike
    Mike avatar
    208 posts
    Registered:
    10 Dec 2007
    03 Oct 2008
    Link to this post
    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
  7. Georgi
    Georgi avatar
    3583 posts
    Registered:
    28 Oct 2016
    06 Oct 2008
    Link to this post
    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.
  8. Jason
    Jason avatar
    21 posts
    Registered:
    12 Nov 2009
    12 Apr 2011
    Link to this post
    will the approach outlined here still work in sitefinity 3.7?  what about v4.0?

    Thanks,
  9. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    12 Apr 2011
    Link to this post
    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

  10. Jason
    Jason avatar
    21 posts
    Registered:
    12 Nov 2009
    12 Apr 2011
    Link to this post
    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.
  11. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    12 Apr 2011
    Link to this post
    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

  12. Jason
    Jason avatar
    21 posts
    Registered:
    12 Nov 2009
    12 Apr 2011
    Link to this post
    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?
  13. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    12 Apr 2011
    Link to this post
    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

  14. Jason
    Jason avatar
    21 posts
    Registered:
    12 Nov 2009
    12 Apr 2011
    Link to this post
    Alright, problem must be on my end :)  thanks again....always get good support in these forums and I do appreciate that.
  15. Jason
    Jason avatar
    21 posts
    Registered:
    12 Nov 2009
    12 Apr 2011
    Link to this post
    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?

  16. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    13 Apr 2011
    Link to this post
    Hi Jason,

    There is a virtual method SetTitle which you can override.

    Kind regards,
    Ivan Dimitrov
    the Telerik team

  17. Antoine
    Antoine avatar
    45 posts
    Registered:
    13 Jul 2012
    09 Jan 2012
    Link to this post
    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); 
            }
     
            ...
       }
    }


  18. Brett Whittington
    Brett Whittington avatar
    89 posts
    Registered:
    10 Aug 2012
    11 Apr 2012
    Link to this post
    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.

  19. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    12 Apr 2012
    Link to this post
    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
  20. Nancy
    Nancy avatar
    35 posts
    Registered:
    10 Apr 2012
    26 Apr 2012
    Link to this post
    How do you do this in Sitefinity 5?
  21. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    09 Dec 2016
    30 Apr 2012
    Link to this post
    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
Register for webinar
21 posts, 0 answered