Copied to clipboard

Flag this post as spam?

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


  • North Krimsly 59 posts 80 karma points
    Jul 23, 2010 @ 00:17
    North Krimsly
    0

    Using htmlEncode in page field

    Greetings all,

    I'm having trouble inserting an Umbraco page field with HTML encoding. The code looks as follows.  Unfortunately it is generating "alternative title & text" rather than "alternative title & text as it should.  Therefore the resulting HTML is invalid.  Using htmlEncode="true" doesn't seem to have an effect.  I'm using version 4.0.4.2.  Any ideas?

    <umbraco:Item field="pageTitle" textIfEmpty="alternative title &amp; text" htmlEncode="true" runat="ser

    Thanks,

    -NorthK

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Jul 23, 2010 @ 12:17
    Sebastiaan Janssen
    0

    Strange! I don't know why this happens, I also tried the html entity & but it still resulted in just an &. But it's not invalid HTML if the ampersand is just somewhere in the text is it? It thought it was just not supposed to be in URL's.

    For lack of a better solution you could create an XSLT macro:

    <xsl:choose>
      <xsl:when test="$currentPage/[data alias='pageTitle'] != ''">
        <xsl:value-of select="$currentPage/[data alias='pageTitle']" />
      <xsl:when>
      <xsl:otherwise>
        <xsl:text>alternative title &amp; text</xsl:text>
      </xsl:otherwise>
    </xsl:choose>
  • North Krimsly 59 posts 80 karma points
    Jul 23, 2010 @ 17:42
    North Krimsly
    0

    Hi Sebastiaan,

    Thanks for the XSLT code-- yes, I could use this but it would be a lot more convenient if the htmlEncode feature worked.  I'm wondering if this is a bug and if so, how I should report it?

    The resulting '&' rather than '&amp;' results in invalid HTML, according to the W3C validator.  I'm using it as part of a <title></title> section in the header.  I also tried urlEncode="true" and that also did not work.

    Thanks,

    -NorthK

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Jul 23, 2010 @ 19:23
    Matt Brailsford
    0

    Hey NorthK,

    That is weird, looking in reflector, it definatley gets passed through HtmlEncode, so not sure why it wouldn't be encoded correctly

    As an alternative, you could try setting the header to:

    <umbraco:Item field="pageTitle" textIfEmpty="alternative title &amp;amp; text" htmlEncode="true" runat="server" />

    Matt

  • North Krimsly 59 posts 80 karma points
    Jul 23, 2010 @ 19:36
    North Krimsly
    0

    Hey Matt,

    You are super-clever-- your suggestion worked great!  The resulting generated code is correct. However, if Umbraco fixes this in the future, having the double ampersand in there will once again produce invalid HTML which I'd like to avoid.

    For that reason it might be a better idea to write an XSLT macro that properly encodes the editible page field for HTML entities.  However, how do I do this in XSLT?  Anyone know?

    Thanks a lot,

    -NorthK

     

  • Matt Brailsford 4125 posts 22223 karma points MVP 9x c-trib
    Jul 23, 2010 @ 20:21
    Matt Brailsford
    0

    Hey NorthK,

    You should be able to use the HtmlEncode library method within XSL to HTML encode a string

    umbraco.library:HtmlEncode(...)

    I've tried a few things with the umbraco:Item, but nothing seems to be working. Best I could suggest would be to create a Macro as you say, or maybe just a user control that you include in your master page and just do it via .NET.

    Matt

  • North Krimsly 59 posts 80 karma points
    Jul 24, 2010 @ 05:53
    North Krimsly
    0

    All,

    As it turned out I didn't need to use HtmlEncode(), since I was storing the page field as a textstring and apparently textstrings are automatically encoded into HTML entities if they need to be.  For some reason I thought textstrings would not do the encoding automatically.  So I did use an XSLT macro which in the end, looks alot like Sebastiaan's code :)  I did get the HtmlEncode() function working so I'll post my code here in case someone needs it (or I need it!).

    <!-- if an SEO page title exists, use it.  If not, use a standard title -->
    <xsl:choose>
        <xsl:when test="$currentPage/data [@alias='pageTitle'] != ''">
            <xsl:value-of select="$currentPage/data [@alias='pageTitle']/text()"/>
        </xsl:when>
        <xsl:otherwise>
            <xsl:text disable-output-escaping="yes"><![CDATA[my page title here with &amp; embedded in it.]]></xsl:text>
        </xsl:otherwise>
    </xsl:choose>

    </xsl:template>
    </xsl:stylesheet>

    <!-- preserving sample code here on how to HTML encode into entities, in case I need it later -->
    <!--        <xsl:variable name="title" select="$currentPage/data [@alias='pageTitle']/text()"/> -->
    <!--        <xsl:value-of select="umbraco.library:HtmlEncode($title)"/> -->

    Thanks a lot guys!

    -NorthK

Please Sign in or register to post replies

Write your reply to:

Draft