Copied to clipboard

Flag this post as spam?

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


  • MartinB 411 posts 512 karma points
    Jun 02, 2012 @ 15:32
    MartinB
    0

    TruncateString breaks html (bug?)

    Hi

    I use TruncateString to limit the number of characters in a teaster text, in a slider gallery (image + text).

    <xsl:value-of select="umbraco.library:TruncateString(teaserText, number($excerptLength), '...')" disable-output-escaping="yes" />

    When "disable-output-escaping="yes" is in the code, my page will literally fall apart with weird half-cut <a> tags that surround

    like:

    <a class="ui...<p class=" infobtncontainer"="" href="#">

    As soon as i remove the output-escaping part, it all works like a charm, except my string is no longer truncated.

    I tried setting the string through a variable first, but no luck. I don't know how a Truncate function is outputting additional <a> tags? I've triple checked that all my tags are close properly, which they are.

    Has anyone tried something similar? I'm on the verge of going mental here ;-)

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Jun 02, 2012 @ 15:37
    Jan Skovgaard
    0

    Hi Martin

    What type of property is your teaser text? Is it a RTE or a multiple text box?

    /Jan

  • MartinB 411 posts 512 karma points
    Jun 02, 2012 @ 15:37
    MartinB
    0

    * That surround my main divs thus making all text below my slider a link to '#'.

  • MartinB 411 posts 512 karma points
    Jun 02, 2012 @ 15:37
    MartinB
    0

    Hi Jan

    Oh sorry, it's RTE

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Jun 02, 2012 @ 15:40
    Jan Skovgaard
    1

    Hi Martin

    Ok, then you should probably use the umbraco.library:StripHtml extension like...

    umbraco.library:Truncate(umbraco.library:StripHtml(teaerText),number(100)).

    Hope this helps :)

    /Jan

  • MartinB 411 posts 512 karma points
    Jun 02, 2012 @ 15:46
    MartinB
    0

    Hi Jan

    I thought about that,but the teasertext has paragraphs, so i would very much like to output the

    tags from the RTE so the 209 character long teaser is nicely output with 2x 3-4 line paragraphs.

    Also, inserting the <p> tags manually is not an option for my client.

     

  • MartinB 411 posts 512 karma points
    Jun 02, 2012 @ 15:59
    MartinB
    0

    To clarify;

    This is how i want it to look, so the <p> tags need to be there, but not shown, hence disable-output-escaping should be there:

  • MartinB 411 posts 512 karma points
    Jun 02, 2012 @ 16:12
    MartinB
    0

    xslt code:

    <!-- start writing XSLT -->
    <div id="slider">
    <a id="prev" href="#" title="Forrige slide"><img src="/css/gfx/previous.png" alt="Forrige slide" class="sliderPrevious" onclick="_gaq.push(['_trackEvent', 'Forside', 'Handlinger', 'Forrige slide klik']);" /></a>
    <a id="next" href="#" title="Næste slide"><img src="/css/gfx/next.png" alt="Næste slide" class="sliderNext" onclick="_gaq.push(['_trackEvent', 'Forside', 'Handlinger', 'Næste slide klik']);" /></a>
    <div id="imageSlider">
    <xsl:for-each select="umbraco.library:GetXmlNodeById($source)/child::umediaSlider [@isDoc and umbracoNaviHide != 1]">
    <div class="imageSlide">
    <div class="imageSliderInfo">
    <p class="aniInfo">Info om skadedyret:</p>
    <h3 class="slide"><xsl:value-of select="@nodeName"/></h3>
    <span><xsl:value-of select="umbraco.library:TruncateString(teaserText, number($excerptLength), '...')" disable-output-escaping="yes" /></span>
    <p class="infoBtnContainer">
    <xsl:if test="string(selectedLink) != ''">
    <a href="{umbraco.library:NiceUrl(selectedLink)}" title="Sådan bekæmpes {@nodeName}" class="CTAinsect">
    <xsl:value-of select="linkText"/>
    </a>
    </xsl:if>
    </p>

    </div>
    <div class="imageSliderImage">
    <img src="{umbracoFile}" width="665" height="283" />
    </div>
    </div>
    </xsl:for-each>
    </div>
    </div>
  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Jun 02, 2012 @ 16:17
    Jan Skovgaard
    1

    Hi Martin

    Well it's definitely not a bug. It makes sense that it just truncates text/html.

    However I currently I can't wrap my head around the proper solution for what you're trying to do. I'll let you know if an idea pops up all of the sudden.

    /Jan

  • MartinB 411 posts 512 karma points
    Jun 02, 2012 @ 16:26
    MartinB
    0

    Hi Jan

    Out of pure interest and lack of knowledge of the truncate function:

    How is it not a bug that a for each loop causes multiple outputs of half-broken a tags when disable-output-escaping="yes" is added to a truncate functionality?

    I'm using this snippet on another site, where there's no problems, also in a for-each loop:

     <div class="newsTextContainer">
    <xsl:choose>
    <xsl:when test="string($excerptLength) != '0'">
    <xsl:value-of select="umbraco.library:TruncateString(introduction, number($excerptLength), '...')" disable-output-escaping="yes" />
    </xsl:when>
    <xsl:otherwise>
    <xsl:value-of select="introduction" disable-output-escaping="yes"/>
    </xsl:otherwise>
    </xsl:choose>
    </div>
  • MartinB 411 posts 512 karma points
    Jun 02, 2012 @ 16:30
    MartinB
    0

    Even if i disable the javascript creating the slide effect, it still happens. It's quite a mindboggle that i have it working on one site, but not the other.

  • Fuji Kusaka 2203 posts 4220 karma points
    Jun 03, 2012 @ 05:25
    Fuji Kusaka
    1

    Hi Martin,

    The Truncate function is not a bug, if you only want plain text from the xslt try this

    <xsl:value-ofselect="umbraco.library:TruncateString(umbraco.library:StripHtml(introduction), 150, '...')"/>


    //fuji

  • MartinB 411 posts 512 karma points
    Jun 03, 2012 @ 11:48
    MartinB
    0

    Hi Fuji

    I'm not refering to the function itself being a bug. My problem is that using truncate in this case breaks the html structure when i add disable-output-escaping="yes"

    It seems like the for-each loop returns more nodes than there are, as soon as i add the above to the value-of string.

    Visuals:

    When i remove disable-output-escaping="yes", these a tags disappear and everything works like intended:

    I'm not interested in plain text as i need the paragraphs for the formatting of the text.

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Jun 03, 2012 @ 13:01
    Lee Kelleher
    2

    Hi Martin,

    The issue at hand is that you would like to truncate the content, and keep the HTML intact, right?  As you know the TruncateString method is only going to truncate a string - it doesn't care whether it's plain-text, HTML or C# code, it's going to cut off the string once it reached the specified number of characters.

    (The screengrabs you posted are from the browser's rendered view - guessing it's from Chrome, but could be Firefox's Firebug ... essentially what you see isn't a true representation of the HTML output, it's the web-browser's interpretation of the HTML, e.g. it will try to auto-correct mistakes)

    The answer to your original problem is to use a method that is going to keep the HTML intact.  Luckily, the guys at CogWorks wrote a blog post about this very thing a while back. Take a read over the post, hopefully it will help. (ignore the fact it uses Blog4Umbraco) http://thecogworks.co.uk/blog/2011/1/21/n-word-preview-for-blog4umbraco

    Good luck!

    Cheers, Lee.

  • MartinB 411 posts 512 karma points
    Jun 03, 2012 @ 15:23
    MartinB
    1

    Hi Lee

    Adding more characters to the truncate function indeed does fix the issue, how sad i didn't figure that part out by myself.

    Anyway, thank you very much for pointing out the obvious and thanks a bunch for the reference!

     

  • MartinB 411 posts 512 karma points
    Jun 03, 2012 @ 15:26
    MartinB
    1

    I think both Jan and Fuji tried to say the same i just didn't get it.

    Thank you all for helping out!

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Jun 03, 2012 @ 15:57
    Lee Kelleher
    1

    Hi Martin,

    Glad that you've found a fix. No worries about things appearing 'obvious' - we are all here to help and learn. :-)

    Cheers, Lee.

  • MartinB 411 posts 512 karma points
    Jun 03, 2012 @ 19:25
    MartinB
    1

    Indeed the friendliest community on the planet!

Please Sign in or register to post replies

Write your reply to:

Draft