Copied to clipboard

Flag this post as spam?

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


  • julius 107 posts 289 karma points
    Aug 30, 2011 @ 17:24
    julius
    0

    Test if an Media Picker field is filled in Umbraco Master page

    Hello,

    I am new to both Umbraco and .net/C#, so this is probably very simple:

    How would I test if a Media Picker field is set or not in my Umbraco Master Page? I have a document type called "Tool Page" and a corresponding template.

    I want to know if the editor chose a background image for the header ("Background Image" Media Picker field in the backoffice). If it is not chosen I want to add a class name to a div. If it is, I want to do nothing.

    It's a pretty simple test. Do I need to create a macro to accomplish this? It's just if simple if else. I normally don't put logic in templates, but this is so small that it kinda feels wrong to create a macro for this.

  • Rich Green 2246 posts 4008 karma points
    Aug 30, 2011 @ 17:46
    Rich Green
    0

    Hi Julius,

    I would create a Macro for this, otherwise there will be logic in your template anyway.

    Rich

  • julius 107 posts 289 karma points
    Aug 30, 2011 @ 21:53
    julius
    0

    But would it be possible to access the Media Picker field directly from the Master page?

    I know I can display data using

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

    But is there a way to get that field without displaying it and testing its value in the master?

  • Rich Green 2246 posts 4008 karma points
    Aug 30, 2011 @ 21:57
  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Aug 30, 2011 @ 21:59
    Jan Skovgaard
    0

    You could access the value from the Media picker in your template by using <umbraco:Item field="mediapickerfield" runat="server"></umbraco:Item>...but all it would return would be a 4 digit id that is reffering to a media node in the media section in Umbraco.

    And you probably want to have the image rendered in your webpage, right? :-)

    This can be done as Rich is saying, with a macro.

    Let us know if you need any examples on how to do it.

    /Jan

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Aug 30, 2011 @ 21:59
    Jan Skovgaard
    0

    ah, too slow :)

    /Jan

  • julius 107 posts 289 karma points
    Aug 30, 2011 @ 22:03
    julius
    0

    Ah, I see, of course.

    It's media so I need to grab the media item separately. I will do that in a macro.

    and if I would want to access the value of for example a text property and use it incode I would have to use inline XSLT?

  • julius 107 posts 289 karma points
    Aug 30, 2011 @ 22:05
    julius
    0

    I was just wondering if something like this is possible in a Master

    <umbraco:Item name="myVarName" field="fieldName"></umbraco:Item>

     

    <%

       if(myVarName != ""){ %>

      myVarName is not empty!

    <%

    }

    %>

  • Rich Green 2246 posts 4008 karma points
    Aug 30, 2011 @ 22:33
    Rich Green
    0

    Hey,

    The last post on here might help you http://our.umbraco.org/forum/templating/templates-and-document-types/15707-if-statement-on-masterpage?p=0#comment57895

    However it looks like you might enjoy Razor, so this blog post may be of interest http://cultiv.nl/blog/2011/2/16/how-to-use-razor-in-umbraco/

    Rich

  • julius 107 posts 289 karma points
    Aug 30, 2011 @ 23:14
    julius
    0

    Hey Rich, that is indeed kind of what I was looking for. You know, sometimes you have two content types and you don't want to make two templates, because their display is nearly identical but differs in just one CSS class name or something. In that case I like to be able to access the current node right in the template and write a quick if/else based on a picture, a piece of text, a nodename, or somethin, but that seems difficult to do. It brings a lot of code to your template and I don't want that. 

    I am now in the process of creating a macro which is a good solution. That Razor post looks interesting too. Razor seems better suited for a case like this. I'll be digging into that in the next project. Thanks.

Please Sign in or register to post replies

Write your reply to:

Draft