Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Yannick Smits 321 posts 718 karma points
    Mar 22, 2011 @ 18:15
    Yannick Smits
    0

    Access Page or Template context

    From a razor file (cshtml) that is loaded in the body, how would I change the page title?

    Only way I found now is to write it to a HttpContext.Item and then write it to the head section on PreRender, but that's a little tedious if you ask me.

  • Sebastiaan Janssen 5045 posts 15476 karma points MVP admin hq
    Mar 22, 2011 @ 18:26
    Sebastiaan Janssen
    0

    There's no other way, can't change the page flow.. You can do it with javascript using document.title though ;-)

  • Yannick Smits 321 posts 718 karma points
    Mar 22, 2011 @ 21:04
    Yannick Smits
    2

    just found a nifty way:

    1. set the <head> section to be runat=server
    2. get page reference from HttpContext
    Here is the razor code for the latter:
        Page page = HttpContext.Current.Handler as Page;
    
        if (page != null)
        {
            page.Title = "custom title";
        }

  • Sebastiaan Janssen 5045 posts 15476 karma points MVP admin hq
    Mar 23, 2011 @ 09:20
    Sebastiaan Janssen
    0

    Hmm, okay, but if you get some weird behaviour then you should probably check the head runat=server bit, I seem to remember that screws up something, but I can't remember what exactly.

  • Yannick Smits 321 posts 718 karma points
    Mar 23, 2011 @ 12:31
    Yannick Smits
    0

    Yeah, runat=sever in the head messes with the spaces in your title. Can be fixed by putting the content inside a asp:placeholder.

    Still a problem I have with the above approach is that the title tag in your template needs to be empty for the trick to work.

  • Yannick Smits 321 posts 718 karma points
    Mar 23, 2011 @ 16:11
    Yannick Smits
    100

    To complete the solution (if anybody is reading). My last problem was overcome by calling this function in the footer of the page. This way the title tag stays empty for modification inside the body area.

    @functions {
        public static void SetPageTitle(dynamic Model)
        {
            Page page = HttpContext.Current.Handler as Page;
    
            if (page != null && page.Title.Trim() == "")
            {
                page.Title = Model.Name;
            }
        }
    }
    
  • Dan Diplo 1554 posts 6205 karma points MVP 5x c-trib
    Oct 28, 2011 @ 13:24
    Dan Diplo
    0

    I was trying to work this out myself - thanks Yannick for doing the "heavy thinking" for me!

  • Jacob 15 posts 96 karma points
    Feb 06, 2013 @ 20:20
    Jacob
    0

    Sorry for bringing up this old thread, but I am new to Umbraco and want to be able to change my page title from a Razor script.

    This function which should be placed in the page footer, do you do this with an inline macro?

    Once its in the footer, how do you use it from the Razor script, I hope someone can find the time to explain it for me, and maybe put in a few code lines on how to do it.

  • gary 385 posts 916 karma points
    Feb 06, 2013 @ 20:35
    gary
    0

    Hi Jacob

    Must admit when I first started to read this thread, became very confused, this applies to early razor in Umbraco.

    If you add a property pageTitle to your documentType either in a masterDocType or in each docType, you can call <title>@Model.PageTitle</title> in your layout and this will give you the page title for each injected page. (The call maybe @CurrentPage.PageTitle dependant upon which version you are using).

    I think it's as easy as that.

    Regards

    Gary

  • Jacob 15 posts 96 karma points
    Feb 06, 2013 @ 21:01
    Jacob
    0

    Hello Gary.

    Sorry, but as I said I am really new to this concept, so it would be great if you could explain this a bit more.

    Would you put <title>@Model.PageTitle</title> in the master template? Then the title is set to @Model.PageTitle, putting <title>test</title> into a template would make for not valid HTML, would it not? Or am I totally misunderstanding you?

    What I have is a Razor script, connected to a macro, and want to be able to call somthing like

    Page.Header.Title = "myTitle"; from inside the script, so it is not a property on the doctype.


  • gary 385 posts 916 karma points
    Feb 06, 2013 @ 21:14
    gary
    0

    Hi Jacob,

    Think we have some crossed wires.

    Ok normally, in masterTemplate <title>@Model.PageTilte</title> will take the page title from the current page being displayed, you will not need to add anything extra into the "page" template as it will exist in the head section of the layout, or master template. So vaild for Html.

    The bit I cannot see is where you will pull your "myTitle" from if it is not a property or properties on the docType somewhere, where will it be generated from?

    Hope this makes a bit more sense, but feel free to clarify.

    Regards G

  • Jacob 15 posts 96 karma points
    Feb 06, 2013 @ 21:21
    Jacob
    0

    Hey again.

    Thanks for taking your time to help.

    If I put   <head><title>@Model.PageTilte</title> in my master template then I for some reason just get the title set to @Model.PageTilte

    Well I am trying to get it to work with the shop package uCommerce, and there you pull a property with @product.DynamicProperty().SeoTitle, so I want to assign this to the title.

     

  • gary 385 posts 916 karma points
    Feb 06, 2013 @ 21:33
    gary
    0

    Ok, this is now a bit more confusing as I have no knowledge of uCommerce and how it is set up.

    I do not think I can really suggest anything more as my knowledge of both uCommerce and webforms is not great and I think this is more of a question about uCommerce than razor.

    Suggest that you close this thread and ask again as something like "getting page title in Ucommerce", this may get you a far quicker and better answer.

    Sorry I can't help more.

    Regards G

  • Jacob 15 posts 96 karma points
    Feb 06, 2013 @ 21:37
    Jacob
    0

    Thanks for the help. 

    I will see if I can figure it out, else I will make a new post.

Please Sign in or register to post replies

Write your reply to:

Draft