Copied to clipboard

Flag this post as spam?

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


  • Donald Swofford 31 posts 101 karma points
    Oct 03, 2014 @ 17:48
    Donald Swofford
    0

    Hide Div if field is empty

    I have the following code inside a template. I would like to hide if the user didnt put any text in for the objectivebox field

    <div class="box">
              <h2>Objective</h2>
              <p><umbraco:Item field="objectivebox" runat="server" /></p>
              </div>
    
  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Oct 03, 2014 @ 18:38
    Dennis Aaen
    0

    Hi Donald,

    Since you have posted it in XSLT, I assume that you are going to use XSLT. So first go to the developer section a create a new XSLT file, and create a associated marco.

    And then your XSLT file should look like this:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
    <xsl:stylesheet
        version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:msxml="urn:schemas-microsoft-com:xslt"
        xmlns:umbraco.library="urn:umbraco.library" xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets" xmlns:Examine="urn:Examine" xmlns:umbraco.contour="urn:umbraco.contour"
        exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets Examine umbraco.contour ">


    <xsl:output method="xml" omit-xml-declaration="yes"/>

    <xsl:param name="currentPage"/>

    <xsl:template match="/">

    <!-- start writing XSLT -->
    <xsl:if test="$currentPage/objectivebox !=''">
        <div class="box">
            <h2>Objective</h2>
            <p><xsl:value-of select="$currentPage/objectivebox" /></p>
        </div>
    </xsl:if>
    </xsl:template>
    </xsl:stylesheet>

    If you call your XSLT something like this Objectivebox then in your template where the data should be print you need to insert this:

    <umbraco:Macro Alias="Objectivebox" runat="server" />

    Hope this helps, if you have any further questions keep asking them.

    /Dennis

  • Donald Swofford 31 posts 101 karma points
    Oct 03, 2014 @ 20:04
    Donald Swofford
    0

    It works great but the only issue I see is when I do type content in it shows up like <p>hi</p>

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Oct 03, 2014 @ 20:12
    Dennis Aaen
    100

    Hi Donald,

    Okay then add disable-output-escaping="yes" to the value-of select.

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
    <xsl:stylesheet
        version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:msxml="urn:schemas-microsoft-com:xslt"
        xmlns:umbraco.library="urn:umbraco.library" xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets" xmlns:Examine="urn:Examine" xmlns:umbraco.contour="urn:umbraco.contour"
        exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets Examine umbraco.contour ">


    <xsl:output method="xml" omit-xml-declaration="yes"/>

    <xsl:param name="currentPage"/>

    <xsl:template match="/">

    <!-- start writing XSLT -->
    <xsl:if test="$currentPage/objectivebox !=''">
        <div class="box">
            <h2>Objective</h2>
            <p><xsl:value-of select="$currentPage/objectivebox" disable-output-escaping="yes" /></p>
        </div>
    </xsl:if>
    </xsl:template>
    </xsl:stylesheet>

    /Dennis

  • Bjarne Fyrstenborg 1280 posts 3990 karma points MVP 7x c-trib
    Oct 04, 2014 @ 18:55
    Bjarne Fyrstenborg
    1

    Hi Donald

    Just some additional info to Dennis' answer..

    In the condition check you can also use normalize-space() to ensure the field doesn't just contain whitespaces: http://our.umbraco.org/documentation/Reference/Api/UmbracoLibrary/

    <xsl:if test="normalize-space($currentPage/objectivebox)">
        <div class="box">
            <h2>Objective</h2>
            <p><xsl:value-of select="$currentPage/objectivebox" disable-output-escaping="yes" /></p>
        </div>
    </xsl:if>

    Cheers,
    Bjarne 

  • Donald Swofford 31 posts 101 karma points
    Oct 06, 2014 @ 15:24
    Donald Swofford
    0

    Thank you so much! It works perfectly!

Please Sign in or register to post replies

Write your reply to:

Draft