Try Now
More in this section

Forums / Designing & Styling / Conditionally Show Field in Custom Module Widget Template

Conditionally Show Field in Custom Module Widget Template

8 posts, 1 answered
  1. Fitzgerald
    Fitzgerald avatar
    9 posts
    25 Jun 2012
    13 Mar 2012
    Link to this post
    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")%>}

  2. SelAromDotNet
    SelAromDotNet avatar
    912 posts
    18 Jul 2012
    13 Mar 2012
    Link to this post
    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!
  3. Fitzgerald
    Fitzgerald avatar
    9 posts
    25 Jun 2012
    13 Mar 2012
    Link to this post
    Looks like that works for me! Thanks!
  4. Fitzgerald
    Fitzgerald avatar
    9 posts
    25 Jun 2012
    14 Mar 2012
    Link to this post
    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!
  5. Stefani Tacheva
    Stefani Tacheva avatar
    718 posts
    19 Oct 2016
    14 Mar 2012
    Link to this post

    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
  6. Richard
    Richard avatar
    64 posts
    09 Oct 2012
    14 Nov 2014 in reply to SelAromDotNet
    Link to this post

    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!

  7. Kyle
    Kyle avatar
    1 posts
    12 Nov 2014
    22 Jan 2015
    Link to this post

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

  8. Steve
    Steve avatar
    3037 posts
    03 Dec 2008
    27 Jan 2015 in reply to Kyle
    Link to this post


    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()) %>' />

8 posts, 1 answered