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.
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.
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.
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.
Hi Julius,
I would create a Macro for this, otherwise there will be logic in your template anyway.
Rich
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?
Hi Julius,
You could try some inline XSLT http://our.umbraco.org/wiki/reference/templates/umbracoitem-element/inline-xslt
Rich
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
ah, too slow :)
/Jan
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?
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!
<%
}
%>
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
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.
is working on a reply...