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.
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.
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;
}
}
}
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.
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).
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.
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.
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.
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.
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.
There's no other way, can't change the page flow.. You can do it with javascript using document.title though ;-)
just found a nifty way:
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.
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.
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.
I was trying to work this out myself - thanks Yannick for doing the "heavy thinking" for me!
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.
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
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.
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
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.
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
Thanks for the help.
I will see if I can figure it out, else I will make a new post.
is working on a reply...