Copied to clipboard

Flag this post as spam?

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


  • Matt Brailsford 4125 posts 22224 karma points MVP 9x c-trib
    Sep 20, 2009 @ 19:50
    Matt Brailsford
    0

    convertLineBreaks with HTML 4.01 strict doc type

    Hi Guys,

    I've added an umbraco:Item control to render some text to the page. I want to replace newlines with <br> tags, however the built in convertLineBreaks method always returns <br/>. The problem is, I'm using the HTML 4.01 strict docytype so need it to output <br> in order the validate. Anyone know how to change it's output?

    Many thanks

    Matt

  • Douglas Robar 3570 posts 4711 karma points MVP ∞ admin c-trib
    Sep 20, 2009 @ 21:04
    Douglas Robar
    1

    I haven't used html (rather than xhtml) in years so I haven't tried this to be sure, but I think that if you used an xslt macro you could get the effect you want.

    Shout if you need more details of the steps, but here is the basic idea...

    Create a new xslt macro and use the 'clean' template. Then add some xsl:value-of statements for the fields you want to display (more on this in a moment). In your template where you now have the umbraco:Item, delete that line and replace it with a call to your macro instead. The macro will create the output and place that output into your template just as the umbraco:Item had done.

    Now, the key with your xslt macro is that you A) want to convert the linebreaks, and B) want the output to be in html-compliant format, not xhtml. The first step is easy as there's a built-in library function (umbraco.library:ReplaceLineBreaks()), and the second is easy because you can specify the output format of your xslt in the xsl:output line. By default it is xml/xhtml but you can also force it to be old-style html output.

    Your macro would look something like the following (replacing 'myfield' with the alias of the property you wish to display and was used as the field= parameter in the original umbraco:Item tag you've replaced with this macro).

    <?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"
        exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets ">

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

    <xsl:param name="currentPage"/>

    <xsl:template match="/">
        <xsl:value-of select="umbraco.library:ReplaceLineBreaks($currentPage/data[@alias='myfield'])"/>
    </xsl:template>

    </xsl:stylesheet>

    You could certainly make this macro more intelligent by passing the field you wish to display and having a propertyType picker as a macro parameter. But the first step is to make it work, then make it work better ;)

    cheers,
    doug.

  • Matt Brailsford 4125 posts 22224 karma points MVP 9x c-trib
    Sep 21, 2009 @ 13:39
    Matt Brailsford
    0

    Thanks doug.

    Thats perfect.

    Matt

  • Douglas Robar 3570 posts 4711 karma points MVP ∞ admin c-trib
    Sep 21, 2009 @ 13:53
    Douglas Robar
    0

    You're welcome!

    If it resolves the problem please be sure to mark the solution for the benefit of next person with the same question.

    cheers,
    doug.

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies