How can I set the document title from inside a Razor macro? I tried using Page.Title, but that seems to be overwritten later on in the umbraco page lifecycle.
The master page is set up like the following (which I believe I got from one of the umbraco starter kits) -
The way I'd do this would be to replace the two <umbraco:Item> controls with an <umbraco:Macro> control instead.
Inside the macro you can just output the same two fields - except for the case(s) where you need to do something different. Then it's always obvious where to look for anything that comes out different than what's stored in those two fields.
I like to do something like this for all of my sites, this way if the "metaTitle" property on any page is left blank, it will use the page name instead.
Just tried using something like you described with metaTitle, but the only properties that show up under Model/CurrentPage are the predefined properties from the Umbraco backend.
Is there a recommended way to programmatically add a property, that I can then read from another macro? So the logic would be like - if there is a programmtically-set value, use that, else show the pageName.
(is there something like the way ViewBag works in MVC where you can just dump some values, and read them elsewhere?)
Yeah, but how to I add a value to "metaTitle" programmatically? Or to the properties dictionary, programmatically? I'm starting to think that this is not possible.
I guess it doesn't need to be an umbraco property. Just somewhere I can put a value, programmatically in one macro, and then read it programmatically in another macro. (like ViewBag in MVC)
I tried Page.PropertyName as a test (like your link suggested), but it seems that the dynamic page property doesn't exist at the time the header macro runs. Maybe because the macro in the page head title section runs before the macro in the body.
But I was able to achieve my desired result, using the macro in the page title section. It reads the querystring value, and displays an appropriate title if there is a querystring value. Otherwise it shows the pageName. So it duplicates some logic, but it works...
Thanks, Amir, and everyone else. You led me down the right track!
Set html document title from Razor macro
How can I set the document title from inside a Razor macro? I tried using Page.Title, but that seems to be overwritten later on in the umbraco page lifecycle.
The master page is set up like the following (which I believe I got from one of the umbraco starter kits) -
So maybe the better question is, how to set the umbraco field "pageName" from inside a Razor macro?
Hi Chris,
The way I'd do this would be to replace the two
<umbraco:Item>
controls with an<umbraco:Macro>
control instead.Inside the macro you can just output the same two fields - except for the case(s) where you need to do something different. Then it's always obvious where to look for anything that comes out different than what's stored in those two fields.
/Chriztian
I like to do something like this for all of my sites, this way if the "metaTitle" property on any page is left blank, it will use the page name instead.
Brilliant, thanks. I didn't realize you could have macros up in the document head.
Thanks, Amir. So @Model.ExampleProp in Razor would correspond to the umbraco field "ExampleProp"?
Yep! That system is convenient for building navigation also, so you can override the page name with a custom field when necessary.
-Amir
Just tried using something like you described with metaTitle, but the only properties that show up under Model/CurrentPage are the predefined properties from the Umbraco backend.
Is there a recommended way to programmatically add a property, that I can then read from another macro? So the logic would be like - if there is a programmtically-set value, use that, else show the pageName.
(is there something like the way ViewBag works in MVC where you can just dump some values, and read them elsewhere?)
Hi Chris,
You could try something like this
Yeah, but how to I add a value to "metaTitle" programmatically? Or to the properties dictionary, programmatically? I'm starting to think that this is not possible.
I think I'm confused as to what you're trying to achieve. Where do you want to programmatically add the property from?
I guess it doesn't need to be an umbraco property. Just somewhere I can put a value, programmatically in one macro, and then read it programmatically in another macro. (like ViewBag in MVC)
I used to do this in XSLT but haven't done it in Razor yet, does this thread help? http://our.umbraco.org/forum/developers/razor/29032-Best-way-to-call-a-macro-from-a-macro
I tried Page.PropertyName as a test (like your link suggested), but it seems that the dynamic page property doesn't exist at the time the header macro runs. Maybe because the macro in the page head title section runs before the macro in the body.
But I was able to achieve my desired result, using the macro in the page title section. It reads the querystring value, and displays an appropriate title if there is a querystring value. Otherwise it shows the pageName. So it duplicates some logic, but it works...
Thanks, Amir, and everyone else. You led me down the right track!
is working on a reply...