Trying to use a conditional statement in web part layout.

Dominic Carvalho asked on October 11, 2018 14:42

Hey, I am trying to use a conditional if statement in my web part layout. I have a boolean property that I want to use to effect content.

The problem is my conditional statement is not being resolved.

For e.g.
{%if (ShowCTA) { %}

MY DIV I WANT TO HIDE

{% }#%}

Boolean property called: ShowCTA, If that's true I want to hide the div. Right now it's just printing out the entire if statement onto the DOM.

Any help will be appreciated. Thanks in advance

Dominic

Recent Answers


Brenden Kehren answered on October 11, 2018 14:48 (last edited on December 10, 2019 02:31)

Your logic is backwards. You currently have if it's true, show it. It needs to be if it's not true or false, show it, else hide it.

Simply update your logic to say:

{%if(!ShowCTA) { %} Now display your content {% } |(identity)GlobalAdministrator%}

0 votesVote for this answer Mark as a Correct answer

Dominic Carvalho answered on October 11, 2018 15:09

Hey Brenden Thanks for the reply and I see what your saying but the actual problem I'm experiencing is that the if statement itself isn't working. It's just printing out the entire if statement including the sorrounding tags i.e. {%%} I'm using a web part layout.

Thanks for the thus far.

Regards, Dominic

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on October 11, 2018 15:22

What is the value of the dropdown list? You're using K# syntax so it should be Text/XML as the value.

Image Text

0 votesVote for this answer Mark as a Correct answer

Dominic Carvalho answered on October 11, 2018 15:35

I thought about that but when I create a new web part and go to the create the new layout it doesn't give me that drop down option ?

Regards, Dominic

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on October 11, 2018 15:43

I beleive webpart layouts are always ASCX. So you have to write ASP.NET code.

<asp:PlaceHolder ID="ph1" runat="server" Visible='<%# !Eval<bool>("ShowCTA")'>
Your visible content here.
</asp:PlaceHolder>
0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.