Copied to clipboard

Flag this post as spam?

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


  • Chris C 43 posts 184 karma points
    Nov 11, 2013 @ 19:46
    Chris C
    0

    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) -

    <title>
         <asp:PlaceHolder runat="server">
             <umbraco:Item ID="Item1" runat="server" Field="pageName"></umbraco:Item>
                 -
             <umbraco:Item ID="Item2" runat="server" Field="siteName" recursive="true"></umbraco:Item>
                </asp:PlaceHolder>
            </title>

    So maybe the better question is, how to set the umbraco field "pageName" from inside a Razor macro? 

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Nov 11, 2013 @ 22:29
    Chriztian Steinmeier
    100

    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

  • Amir Khan 1287 posts 2744 karma points
    Nov 11, 2013 @ 23:22
    Amir Khan
    0

    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.

     


    @inherits umbraco.MacroEngines.DynamicNodeContext
    @if(@Model.HasProperty("metaTitle") && @Model.GetProperty("metaTitle").Value != String.Empty)  
            {
              @Model.metaTitle;
            }
          else {
            @Model.Name;
          }

  • Chris C 43 posts 184 karma points
    Nov 11, 2013 @ 23:44
    Chris C
    0

    The way I'd do this would be to replace the two umbraco:Item controls with an umbraco:Macro control instead.

    Brilliant, thanks. I didn't realize you could have macros up in the document head.

    @Model.metaTitle;

    Thanks, Amir. So @Model.ExampleProp in Razor would correspond to the umbraco field "ExampleProp"?

  • Amir Khan 1287 posts 2744 karma points
    Nov 11, 2013 @ 23:47
    Amir Khan
    0

    Yep! That system is convenient for building navigation also, so you can override the page name with a custom field when necessary.

     

    -Amir

  • Chris C 43 posts 184 karma points
    Nov 12, 2013 @ 00:40
    Chris C
    0

    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?)

  • Fuji Kusaka 2203 posts 4220 karma points
    Nov 12, 2013 @ 07:55
    Fuji Kusaka
    0

    Hi Chris,

    You could try something like this

    var tit = CurrentModel.HasProperty("metaTitle") && CurrentModel.GetProperty("metaTitle").Value != String.Empty ? model.GetProperty("metaTitle").Value : CurrentModel.Name;
    @tit
  • Chris C 43 posts 184 karma points
    Nov 12, 2013 @ 18:35
    Chris C
    0

    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. 

  • Amir Khan 1287 posts 2744 karma points
    Nov 12, 2013 @ 18:49
    Amir Khan
    0

    I think I'm confused as to what you're trying to achieve. Where do you want to programmatically add the property from?

  • Chris C 43 posts 184 karma points
    Nov 12, 2013 @ 23:23
    Chris C
    0

    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)

  • Amir Khan 1287 posts 2744 karma points
    Nov 12, 2013 @ 23:28
    Amir Khan
    0

    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

  • Chris C 43 posts 184 karma points
    Nov 13, 2013 @ 00:57
    Chris C
    0

    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!

     

Please Sign in or register to post replies

Write your reply to:

Draft