Copied to clipboard

Flag this post as spam?

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


  • Tom Cowling 144 posts 343 karma points
    Jun 04, 2015 @ 13:33
    Tom Cowling
    0

    Quick variable test

    Hey,

    I know I'm missing something blindingly obvious but my brain just isn't playing ball today.I want to do a simple if statement with an AND operator. I'm doing a test to see if the page has an image. If it does, I want to display a smaller banner. If it doesn't have an image selected, I want to make the banner the full width. I'm doing this with a variable named 'bannersize'.

    It seems like I'm not testing for the variable's value correctly as the result is a YSOD. Any help would be much appreciated!

    Thanks,

    Tom

     

       @{
           
        var bannersize = "small";
           
        if(Model.Content.HasValue("logoImage")){
            var mediaItem = Umbraco.TypedMedia(Model.Content.GetPropertyValue("logoImage"));
            @mediaItem.GetPropertyValue(   
            bannersize = "small";
        }
        else
        {
            bannersize = "large";
        }
           
    if(Model.Content.HasValue("cbannerImage") && bannersize = "small"){
    var mediaItem = Umbraco.TypedMedia(Model.Content.GetPropertyValue("cbannerImage"));
    <img class="floatright" src="@mediaItem.GetPropertyValue("umbracoFile")" height="232" alt="@mediaItem.GetPropertyValue("Name")"/>
    }
    }
  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jun 04, 2015 @ 14:48
    Dennis Aaen
    0

    Hi Tom,

    I just had a look into your code, and I think I have corrected your code so it will works.

    Try to use this code below instead and see if it works like intended.

     @{
    
        var bannersize = "small";
    
        if(Model.Content.HasValue("logoImage")){
            var mediaItem = Umbraco.TypedMedia(Model.Content.GetPropertyValue("logoImage")); 
              <img src="@mediaItem.GetPropertyValue("umbracoFile")" alt="@mediaItem.GetPropertyValue("Name")"/>    
            @bannersize
        } 
        else
        {
            bannersize = "large";
            @bannersize
        }
    
        if(Model.Content.HasValue("cbannerImage") && bannersize == "small"){
            var mediaItem = Umbraco.TypedMedia(Model.Content.GetPropertyValue("cbannerImage")); 
            <img class="floatright" src="@mediaItem.GetPropertyValue("umbracoFile")" height="232" alt="@mediaItem.GetPropertyValue("Name")"/>    
        } 
    }
    

    Hope this helps,

    /Dennis

  • Tom Cowling 144 posts 343 karma points
    Jun 04, 2015 @ 15:04
    Tom Cowling
    0

    It works, but the @bannersize then displays the value of the variable in the page as plain text..?

    Does it need to be rendered as part of the page for Umbraco to use the variable? If it does, I guess I can hide it with css.

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jun 04, 2015 @ 15:18
    Dennis Aaen
    0

    Hi Tom,

    Just to be sure what do you need the large and small banner variable for, then I am sure we can find a way of doing it :-)

    /Dennis

  • Tom Cowling 144 posts 343 karma points
    Jun 04, 2015 @ 15:30
    Tom Cowling
    0

    I was just using the variable to choose what to render.

    The user has the option to pick a couple of images. If they pick an image for slot 1 and 2, I need to make them a bit smaller. If they pick just slot 2, it will fill up the space used slot 1 and 2 sit in. I figured the easiest way was to check if slot 1 was filled and if not, to make slot 2 bigger.

     

    Do you think this would be better achieved with a case rather than an if statement? It just seems a little odd that I can create a variable but not use it in my if statement unless the contents are rendered somewhere?

     

    Thanks for the quick response by the way!

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jun 04, 2015 @ 21:54
    Dennis Aaen
    100

    Hi Tom,

    Okay so if they pick an image for slot 1 and 2, you need to make them a bit smaller. I was thinking if you then just could do something like this.

    @{
        if(Model.Content.HasValue("cbannerImage") && Model.Content.HasValue("logoImage")){
            var mediaItem = Umbraco.TypedMedia(Model.Content.GetPropertyValue("cbannerImage")); 
            <img class="floatright" src="@mediaItem.GetPropertyValue("umbracoFile")" width="320" height="232" alt="@mediaItem.GetPropertyValue("Name")"/>    
        }else{ 
            // Do something
        }
    }
    

    So you check of if slot 1 and 2 then you have pictures in one size else then you can set another width and height

    Hope this helps,

    /Dennis

  • Tom Cowling 144 posts 343 karma points
    Jun 05, 2015 @ 10:43
    Tom Cowling
    0

    Good point. I was overcomplicating things in my head!

    Cheers Dennis.

     

    One thing that's still baffling me slightly though is why I couldn't test against a variable unless it is dropped into the page. Is that a razor thing or an Umbraco thing or just the way I was approaching it?

     

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jun 05, 2015 @ 11:08
    Dennis Aaen
    0

    Hi Tom,

    You can also do this. I think that you could use the code below.

    @{
    
        var bannersizeSmall = "small";
        var bannersizeLarge = "large";
    
        if(Model.Content.HasValue("logoImage")){
            var mediaItem = Umbraco.TypedMedia(Model.Content.GetPropertyValue("logoImage")); 
              <img src="@mediaItem.GetPropertyValue("umbracoFile")" alt="@mediaItem.GetPropertyValue("Name")"/>    
    
        } 
        else
        {
    
        }
    
        if(Model.Content.HasValue("cbannerImage") && bannersizeSmall == "small"){
            var mediaItem = Umbraco.TypedMedia(Model.Content.GetPropertyValue("cbannerImage")); 
            <img class="floatright" src="@mediaItem.GetPropertyValue("umbracoFile")" height="232" alt="@mediaItem.GetPropertyValue("Name")"/>    
        } 
    }
    

    Hope this helps, and make sense.

    /Dennis

  • Tom Cowling 144 posts 343 karma points
    Jun 05, 2015 @ 11:10
    Tom Cowling
    0

    So if I went back to my original post and added in a double equals, it would have worked?

    Sorry - didn't even notice you'd added that into the second post! Long day.

     

    Thanks for all your help.

  • Dennis Aaen 4500 posts 18255 karma points admin hq c-trib
    Jun 05, 2015 @ 11:17
    Dennis Aaen
    0

    Hi Tom,

    Yes I think so, but beware that you need to make two variables, therefor I have changed the names to

    var bannersizeSmall = "small";
    var bannersizeLarge = "large";
    

    And then you can test against this with double equals,

    /Dennis

Please Sign in or register to post replies

Write your reply to:

Draft