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 & text" htmlEncode="true" runat="ser
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 & text</xsl:text>
</xsl:otherwise>
</xsl:choose>
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 '&' 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.
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?
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.
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 & 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)"/> -->
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?
Thanks,
-NorthK
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:
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 '&' 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
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; text" htmlEncode="true" runat="server" />
Matt
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
Hey NorthK,
You should be able to use the HtmlEncode library method within XSL to HTML encode a string
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
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!).
Thanks a lot guys!
-NorthK
is working on a reply...