Copied to clipboard

Flag this post as spam?

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


  • Steve 472 posts 1216 karma points
    Aug 11, 2015 @ 16:51
    Steve
    0

    If Statement to Check for "True / False"

    I've been banging my head on this. I can't seem to get a check for a doctype property of true/false to work. What is wrong with my syntax?

        @using umbraco.MacroEngines
    @inherits umbraco.MacroEngines.DynamicNodeContext
    @{
        var i = 0;
        var nodeId = Parameter.nodeId;
    
    }
    <div class="slider">
    @foreach (var item in Model.NodeById(nodeId).Children.Where("Visible") ){
        if ( item.HasValue("image") ){
        var location = (item.titleLocation != "") ? @item.titleLocation : "top-left";
    
        <div class="slide" id="@i">
            <h3 class="slideTitle @location">@item.name</h3>
        @if ( item.isVideo != true || item.isSlideshow != true ) {
            <a onclick="ga('send', 'event', '@Model.Name Carousel', 'click', 'Link to Carousel Item @i - Story')" href="@item.imageLink"><img src="@item.image" title="@item.name" alt="@item.name" /></a>
                } 
        else if ( item.isSlideshow == true ) {
            <a class="isSlideshow" onclick="ga('send', 'event', '@Model.Name Carousel', 'click', 'Link to Carousel Item @i - Slideshow')" href="#">
                <img src="@item.image" title="@item.name" alt="@item.name" />
            </a>
                } else {
            <a class="video" onclick="ga('send', 'event', '@Model.Name Carousel', 'click', 'Link to Carousel Item @i - Video')" href="@item.videoURL?autoplay=1&modestBranding=0&autohide=1&showinfo=0&rel=0&fs=0&theme=light">
                <img src="/media/1362779/videooverlay.png" class="overlay" height="364" width="750" alt="Play Video for @item.name" />
                <img src="@item.image" title="@item.name" alt="@item.name" />
            </a>
    
        }
            <div class="caption">@item.copy</div>
        </div>
        i++;
        }
    }
    </div>
    
  • Clickfarm 77 posts 161 karma points
    Aug 11, 2015 @ 18:31
    Clickfarm
    0

    are you sure the property type is boolean?

    try

    if ( !bool.Parse(item.isVideo) || !bool.Parse(item.isSlideshow)

  • Steve 472 posts 1216 karma points
    Aug 11, 2015 @ 18:36
    Steve
    101

    Yes, I just now got it to work, but the fact that I had to do it exactly this way is crazy. Here is the only way I could it it to test properly.

        @using umbraco.MacroEngines
    @inherits umbraco.MacroEngines.DynamicNodeContext
    @{
        var i = 0;
        var nodeId = Parameter.nodeId;
    
    }
    <div class="slider">
    @foreach (var item in Model.NodeById(nodeId).Children.Where("Visible") ){
        if ( item.HasValue("image") ){
        var location = (item.titleLocation != "") ? @item.titleLocation : "top-left";
    
        <div class="slide" id="@i">
            <h3 class="slideTitle @location">@item.name</h3>
    
        @if ( (item.isVideo || item.isSlideshow) != true ) {
            <a onclick="ga('send', 'event', '@Model.Name Carousel', 'click', 'Link to Carousel Item @i - Story')" href="@item.imageLink"><img src="@item.image" title="@item.name" alt="@item.name" /></a>
                } 
        else if ( item.isSlideshow ) {
            <a class="isSlideshow" onclick="ga('send', 'event', '@Model.Name Carousel', 'click', 'Link to Carousel Item @i - Slideshow')" href="#">
                <img src="@item.image" title="@item.name" alt="@item.name" />
            </a>
                } else {
            <a class="video" onclick="ga('send', 'event', '@Model.Name Carousel', 'click', 'Link to Carousel Item @i - Video')" href="@item.videoURL?autoplay=1&modestBranding=0&autohide=1&showinfo=0&rel=0&fs=0&theme=light">
                <img src="/media/1362779/videooverlay.png" class="overlay" height="364" width="750" alt="Play Video for @item.name" />
                <img src="@item.image" title="@item.name" alt="@item.name" />
            </a>
    
        }
            <div class="caption">@item.copy</div>
        </div>
        i++;
        }
    }
    </div>
    
  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Aug 12, 2015 @ 10:00
    Sebastiaan Janssen
    0

    I don't know what version of Umbraco you're using but I thing in v6 and above you should be able to do something like (code abbreviated):

        @using Umbraco.Web
        @{
            var umbracoHelper = new UmbracoHelper(UmbracoContext.Current);
            var myNode = umbracoHelper.TypedContent(1234);
        }
    
        @foreach (var item in myNode.Children.Where(x => x.IsVisible()))
        {
            if (item.HasValue("image"))
            {
                if (!item.GetPropertyValue<bool>("isVideo") || !item.GetPropertyValue<bool>("isSlideshow"))
                {
                    // and so on
                    // you can get properties of type string like so:
                    // item.GetPropertyValue<string>("myPropertyAlias")
                    // or int: 
                    // item.GetPropertyValue<int>("myPropertyAlias")
                    // etc.
                }
            }
        }
    

    It is a little more verbose but you also get a lot more intellisense and code completion if you're working in Visual Studio. This also gets you out of the realm of dynamics which don't perform nearly as well.

    Again, if this works depends on your VS version but it might benefit you.

  • Steve 472 posts 1216 karma points
    Aug 12, 2015 @ 12:23
    Steve
    0

    Thanks Sebastiaan. I don't think I've seen this way of using the "type" of the returned value of the property. But it's good to know going forward.

Please Sign in or register to post replies

Write your reply to:

Draft