Copied to clipboard

Flag this post as spam?

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


  • Alex Kørup 17 posts 39 karma points
    May 02, 2012 @ 16:48
    Alex Kørup
    0

    How to output a conditional field?

    Outputting a field in Umbraco is pretty straight forward:

    <umbraco:Item runat="server" field="introduction">
    

    Lets say I'm currently rendering an article with the property 'introduction'. In case the article introduction field is empty I want instead to output a more global field on the home site node, let's call it 'global_introduction'.

    Does anybody know how to do this?

    I know about the optional attribute xslt, but don't know if it will suffice... or exactly how to do it.

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    May 02, 2012 @ 20:06
    Jan Skovgaard
    0

    Hi Alex

    If you want to keep using the umbraco:Item way of doing things all you need to to is add another attribute named "useIfEmpty" and then type in the property you want to display instead. Like this: <umbraco:Item field="introduction" useIfEmpty="bodyText" runat="server" />

    If you edit the templates using the editor build into Umbraco you can see the different field options by clicking the button right next to the "save" icon.

    Hope this helps.

    /Jan

  • Alex Kørup 17 posts 39 karma points
    May 02, 2012 @ 20:45
    Alex Kørup
    0

    Thank you for your reply. The useIfEmpty selects a field in the same scope as the field="introduction" attribute, right? Well, what I need if the 'introduction' is empty is to select a global field set on the top node in the content hierarchy (ie. the frontpage of my site).

    I assume the useIfEmpty doesn't allow XPath!?

    /Alex

     

     

  • Mike Chambers 636 posts 1253 karma points c-trib
    May 02, 2012 @ 22:45
    Mike Chambers
    0

    What if you set recursion? I'm not entirely sure how the useIfEmpty and recurse work in tandem... but might be worth investigating??

    <umbraco:Item field="introduction" useIfEmpty="bodyText" recursive="true" runat="server" />

    Or just jump out into macro, with more powerful logic, rather than try and use the simple umbraco item.

  • Alex Kørup 17 posts 39 karma points
    May 03, 2012 @ 13:31
    Alex Kørup
    0

    Setting recursive=true doesn't seem to effect the useIfEmpty value. :( This would otherwise have been what I needed!

    Maybe a macro is the only way around this issue...

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies