Conditionally Show Field in Custom Module Widget Template - Front- & Back-End Development - Front- & Back-End Development - Progress Community
 Front- & Back-End Development

Conditionally Show Field in Custom Module Widget Template

  • Conditionally Show Field in Custom Module Widget Template
  • 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>
        <%# Eval("Title")%>

  • 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!
  • Looks like that works for me! Thanks!
  • 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!
  • 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.

    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
  • 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>
    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">
            <section id="boxContainer">
            <div class="boxes" >
              <asp:PlaceHolder ID="ItemsContainer" runat="server" />
        <article class="<%#Eval("BoxStyle") %>" data-w="<%#Eval("DataW") %>" data-h="<%#Eval("DataH") %>">
            <%#Eval("Content") %> 
                    <span class="caption simple-caption">
                <%#Eval("Caption") %>

    Thanks for your help!

  • 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


     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"/>

  • @Kyle

    What about something like 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()) %>' />