Copied to clipboard

Flag this post as spam?

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


  • Jason Kunkle 13 posts 123 karma points
    Oct 12, 2023 @ 14:34
    Jason Kunkle
    0

    Issue with using an if statment to conditionally display a header in a template

    In my template I'm trying to check if my header content item has a value. If it has a value I want it to be displayed as an h2, if it does not have a value I don't want to display it. I tried the code below but it does not work. What would be the proper syntax for this?

    var leftHeader = item.Value<IPublishedContent>("interiortLeftHeader");
    @if(leftHeader != ""))    
    {   <h2>@item.Value("interiortLeftHeader")</h2>   }
    

    Thank you.

  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    Oct 12, 2023 @ 19:01
    Marc Goodson
    100

    Hi Jason

    It depends what type of property editor is, I'm going to assume it is a textstring as tgat would be normal for a header... Then

    @if (item. HasValue("interiorLeftHeader")) { <h2>@(item.Value<string>("interiortLeftHeader")) </h2> }

    Would I think be what you are after?

    Regards

    Marc

  • Jason Kunkle 13 posts 123 karma points
    Oct 12, 2023 @ 19:05
    Jason Kunkle
    0

    That worked perfectly!

    Thank you!

  • Marc Goodson 2155 posts 14408 karma points MVP 9x c-trib
    Oct 12, 2023 @ 19:12
    Marc Goodson
    0

    That's good, because I was typing on my mobile and it took ages to escape the code sample with & lt; etc!

Please Sign in or register to post replies

Write your reply to:

Draft