Copied to clipboard

Flag this post as spam?

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


  • Nicky Christensen 76 posts 166 karma points
    Jan 09, 2012 @ 11:09
    Nicky Christensen
    0

    Get logo recursively wont work?

    Hey guys...
    Im trying to get a logo recursively - however, it wont work as i want it to...

    I have 4 entrances
    Home
      - Entrance1
    - A page

    - Entrance2
    - Entrance3
    - Entrance4

    Each entrance has it's own logo added, so it should overwrite what is on the home node - However it doesnt... 
    My code looks like

    @inherits umbraco.MacroEngines.DynamicNodeContext

    @if (Model.AncestorOrSelf().HasProperty("siteLogo")){

      <a class="logo" href="/"><img src='@Model.AncestorsOrSelf().FirstOrDefault().Media("siteLogo", "umbracoFile")' /></a>

    } else {

      <a class="logo" href="/"><img src='@Model.Media("siteLogo", "umbracoFile")' /></a>

    }

    If i use FirstOrDefault() it always takes the logo from the home node,if i use LastOrDefault i outputs nothing on fx the item "Mypage.aspx" because i havent added a logo to this item, cuz it should inherit it from "Entrance1"? 
    Any suggestions?

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Jan 09, 2012 @ 14:35
    Tom Fulton
    1

    Hi,

    You could try something like this instead.  The true on GetPropertyValue indicates it should get the value recursively:

    @{
    var mediaId = Model.GetPropertyValue("siteLogo", true);
    var media = Library.MediaById(mediaId);
    }
    <a class="logo" href="/"><img src="@media.umbracoFile" /></a

    -Tom

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Jan 09, 2012 @ 19:49
    Sebastiaan Janssen
    2

    Or, even easier:

    var mediaId = Model._siteLogo;

    By adding the underscore in front of the property name, you're also saying that you want it recursively. However, there MIGHT be a bug in 4.7.1 preventing either Tom's or my solution to work, so try it out and if it doesn't work, 4.7.1.1 (with a fix) should be out this week.

  • Funka! 398 posts 661 karma points
    Jan 10, 2012 @ 02:31
    Funka!
    0

    The underscore in front of the Razor property name is something I wished I had known about a while ago. I only very recently learnt this after picking it up as a short aside in one of the "what's new in v5 beta" posts. I don't recall seeing it in any of the Umbraco TV episodes or anywhere else fairly obvious so it does seem like a nice secret to know!

    Good luck!

  • Nicky Christensen 76 posts 166 karma points
    Jan 12, 2012 @ 09:25
    Nicky Christensen
    0

    Thx for the answers - Didn't know about the underscore in front of the property name :) Kindda funny, but easy way to say that you awnt something recursively...

  • Michael Latouche 504 posts 819 karma points MVP 4x c-trib
    Jan 12, 2012 @ 10:13
    Michael Latouche
    1

    For those interested, there is a very good razor DynamicNode cheatsheet available at http://our.umbraco.org/projects/developer-tools/razor-dynamicnode-cheat-sheet with lots of other interesting stuff :-)

    Cheers,

    Michael.

  • Simon Dingley 1474 posts 3431 karma points c-trib
    Feb 15, 2012 @ 12:23
    Simon Dingley
    0

    In 4.7.1.1 this seems to return empty strings if it hits a node with the property but no value set - is this by design and how do you work around it telling it to only return a property that has a real value set?

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Feb 15, 2012 @ 14:27
    Tom Fulton
    0

    Same issue here using Model.GetPropertyValue("prop", true) - but Sebestiaan's method still seems to work fine I believe - Model._prop.  Is that working for you also?  Sounds like a bug, maybe we can check with @agrath

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Feb 15, 2012 @ 15:09
    Sebastiaan Janssen
    1

    I remember there was a problem with the AncestorOrSelf call that should've been fixed in 4.7.1.1, but Model._prop should indeed work.

  • Simon Dingley 1474 posts 3431 karma points c-trib
    Feb 15, 2012 @ 15:39
    Simon Dingley
    0

    Underscore trick did it - thanks.

Please Sign in or register to post replies

Write your reply to:

Draft