Conditionally Show Field in Custom Module Widget Template

Posted by Community Admin on 05-Aug-2018 16:44

Conditionally Show Field in Custom Module Widget Template

All Replies

Posted by Community Admin on 13-Mar-2012 00:00

I've created a custom module citation for academic references. Citations have, among other things, a title and an optional articleURL to link to the original article.

Is there a way for me to write the widget template so that, if the URL is not blank, render it as a link? With apologies for my PHP style pseudocode (new to sitefinity/.net), what I have in my head is something like:

if (notEmpty('articleURL'))
    <a href="<%# Eval("articleURL")%>"><%# Eval("Title")%></a>
 else 
    <%# Eval("Title")%>

Posted by Community Admin on 13-Mar-2012 00:00

Hi Fitzgerald,

by default, the built-in widget template editor will strip any server side code so this approach unfortunately won't work.

However, I believe there are two ways to achieve the desired result.

You might be able to use the tertiary operator to hide a value, such as:

<%# Eval("Foo") == null ? "" : Eval("Foo") %>

Alternatively, you can map the widget template to an external file. Using an external template file will not strip the code, and you have full control to render the content however you wish.

For more information on mapping templates, take a look at this post: Mapping External Templates for Sitefinity 4 Widgets

hope this is helpful!

Posted by Community Admin on 13-Mar-2012 00:00

Looks like that works for me! Thanks!

Posted by Community Admin on 14-Mar-2012 00:00

Is there documentation for the UI widgets in the 'sf' namespace? I'm looking for some kind of reference to all the <sf:...> controls.

Thanks again!

Posted by Community Admin on 14-Mar-2012 00:00

Hello,

To create an instance of the control, declare the appropriate namespace in your user control.
You should use the following source code and change the tag prefix and namespace to what you want to be used.

<%@ Register TagPrefix="sf" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>


If you use sf tag prefix, when you write <sf:, you will see all the user controls which are used in the namespace you have indicated. You could read the following document.
The  Namespace="Telerik.Web.UI" could be any namespace that contains user controls change the namespace with a prefered one type sf: and you will see a list of all available controls.

Regards,
Stefani Tacheva
the Telerik team
Do you want to have your say in the Sitefinity development roadmap? Do you want to know when a feature you requested is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items

Posted by Community Admin on 14-Nov-2014 00:00

Hey I know this is a couple years down the road, but I'm in need of this functionality. As a newbie programmer I really don't know how to implement an 'if/else' type of scenario, especially in Sitefinity.

What I have is a Featured Item that may or may not have a caption that shows up on hover. I built this module using Module Builder. There is a 'caption' field that they could enter a caption into. What I need though is to make it so that if the user doesn't enter a caption the code for the caption won't show up. I have this tiny bit of code as the caption

<span class="caption simple-caption">
    <p>(Simple Caption)</p>
</span>
and if it shows up when they didn't enter a caption it messes things up. So I'd like to implement a conditional somehow... you mention that if I map the widget template to an external file I can use 'conditional' code that won't get stripped out. So based on that I have two questions: How do I map the widget template to an external file and what would be an example of the sample code?

Currently this is the code I have for the  'RadListView' item for the Widget Template that the Featured Items go into.

<telerik:RadListView ID="dynamicContentListView" ItemPlaceholderID="ItemsContainer" runat="server" EnableEmbeddedSkins="false" EnableEmbeddedBaseStylesheet="false">
    <LayoutTemplate>
        <section id="boxContainer">
        <div class="boxes" >
          <asp:PlaceHolder ID="ItemsContainer" runat="server" />
                </div>
         </section>
    </LayoutTemplate>
    <ItemTemplate>
    <article class="<%#Eval("BoxStyle") %>" data-w="<%#Eval("DataW") %>" data-h="<%#Eval("DataH") %>">
        <%#Eval("Content") %> 
                <span class="caption simple-caption">
            <%#Eval("Caption") %>
        </span>
    </article>
    </ItemTemplate>
</telerik:RadListView>


Thanks for your help!

Posted by Community Admin on 22-Jan-2015 00:00

I have created a module for bio information.  I want to drop the widget on a page and only display items if they have values.  Right now, regardless if there is data in the in the field, the label is still showing, ex:

 Name : John Smith

University:

 Phone: 412-214-5433

 I want to hide the University label because it's empty.  Here is the code from the widget template for that particular field/label.

 

<div class='sfitemShortTxtWrp'>        
                <sf:SitefinityLabel runat="server" Text='University:' WrapperTagName="div" HideIfNoText="true" CssClass="sfitemFieldLbl bio-text-dt" />        
                <sf:SitefinityLabel runat="server" Text='<%# Eval("University")%>' WrapperTagName="div" HideIfNoText="true" CssClass="sfitemShortTxt bio-text-dd" data-sf-field="University" data-sf-ftype="ShortText"/>
            </div>

Posted by Community Admin on 27-Jan-2015 00:00

@Kyle

What about something like this...link the visibility of the prev label to the text of the University field

<sf:SitefinityLabel runat="server" Text='University:' WrapperTagName="div" CssClass="sfitemFieldLbl bio-text-dt" Visible='<%# !String.IsNullOrEmpty(Eval("University").ToString()) %>' />

This thread is closed