1

I want to do a check if something is an integer, so if it is 8, then assign a certain value to a variable, if not, another variable, pretty straightforward, but I get an error on this?

var getColor = umbraco.presentation.nodeFactory.Node.GetCurrent().GetProperty("textColor" + i).Value;
                if (getColor == 8)
                {
                    var color = "black";
                }
                else
                {
                    var color = "white";
                } 

Then I want to print out the variable in my HTML as a class like so:

<div class="headline <% color %>"></div>

The whole code looks like:

<div class="slides" onmouseleave="startSlider();">
            <%for(int i = 1; i < 5; i++) { %>
            <%

                var getColor = umbraco.presentation.nodeFactory.Node.GetCurrent().GetProperty("textColor" + i).Value;
                if (getColor == "8")
                {
                    var color = "black";
                }
                else
                {
                    var color = "white";
                } 
            %>
                <%if(umbraco.presentation.nodeFactory.Node.GetCurrent().GetProperty("image"+ i).Value != "") {%>
                    <div class="slide" id="slide<%=i%>" onclick="location.href = '<%=umbraco.library.NiceUrl(int.Parse(umbraco.presentation.nodeFactory.Node.GetCurrent().GetProperty("link"+ i).Value))%>';" style="display:<%if(i != 1){%>none<%}%>;" tmpcolor="<%=umbraco.presentation.nodeFactory.Node.GetCurrent().GetProperty("backgroundColor"+ i).Value%>">
                    <div class="image" id="image<%=i%>" style="background: url(<%=umbraco.presentation.nodeFactory.Node.GetCurrent().GetProperty("image"+ i).Value%>) center center;"></div>
                    <div class="headline <%=color%>"><%=umbraco.presentation.nodeFactory.Node.GetCurrent().GetProperty("headline"+ i).Value%></div>
                    <div class="subline"><%=umbraco.presentation.nodeFactory.Node.GetCurrent().GetProperty("subline"+ i).Value%></div>
                    <div class="menuitems">
                        <%for(int y = 1; y < 5; y++) { %>
                            <%if(umbraco.presentation.nodeFactory.Node.GetCurrent().GetProperty("menutext"+ y).Value != "") {%><span class="item<%if(i==y){%> selected<%}%>" onmouseover="gotoSlide(<%=y%>);"><a title="<%=umbraco.presentation.nodeFactory.Node.GetCurrent().GetProperty("headline"+ i).Value%>" href='<%=umbraco.library.NiceUrl(int.Parse(umbraco.presentation.nodeFactory.Node.GetCurrent().GetProperty("link"+ y).Value))%>'><%=umbraco.presentation.nodeFactory.Node.GetCurrent().GetProperty("menutext"+ y).Value%></a><br/></span><%}%>
                        <%}%>
                    </div>
                </div>
                <%}%>
            <%}%>
        </div>

Im getting the error that "color in not in the current context"

  • What error are you getting, and what version of umbraco are you using? Secondly, I'm assuming you have properties on your document type called textColor1, textColor2, etc etc etc? Did you publish the content since you added the properties? New properties won't show up on content until the content has been published since they were added, (e.g. republished). – Ryan Mann Oct 20 '14 at 17:38
  • Yeah, the propteries in Umbraco is there as textColor1 etc. Im using an older version of Umbraco - Im getting an error where i get color is not in the currrent context... –  Oct 20 '14 at 17:40
  • where is the above code running? In a razor view? Controller? WebForm (e.g. aspx ascx?) – Ryan Mann Oct 20 '14 at 17:41
  • Can you tell me what version exactly of umbraco you are running, the api changes a lot from version 3 - 5 - 6 -7.... – Ryan Mann Oct 20 '14 at 17:47
  • Yeah, it umbraco 6.1 –  Oct 20 '14 at 17:49

1 Answers1

0

Value returns a string, which is dependent on the datatype you used for the properties on your document type. As such you can't say "getColor == 8".

Here are some snippets I got working in a test environment of mine on a .master or .aspx page.

<script runat="server">
    string GetColorClass()
    {
        var color = umbraco.NodeFactory.Node.GetCurrent().GetProperty("textColor1").Value;
        color = color == null ? string.Empty : color;
        switch (color)
        {
            case "8":
                return "black";
            default:
                return "white";
        }
    }
</script>

<div class="<% =GetColorClass() %>">
    <h3>Cool It Works!</h3>
</div>

Now, also notice I am using <% =... versus what you had as <%.

<% %> = inline code (doesn't insert anything into the response, just executes inline code) <%$ %> = for evaluating expressions with one of the expression builders registered in the web.config ...

<%= %> : short hand for response.write (this is what you want here because it will insert the output into the response where encountered, e.g. like on a class=" attribute.

For a more complete list of these, see the post I took this from, <%$, <%@, <%=, <%# ... what's the deal?

I also used the node factory from a different namespace, the namespace you were accessing it from has been marked obsolete and could be removed from future versions.

Community
  • 1
  • 1
Ryan Mann
  • 5,178
  • 32
  • 42