Copied to clipboard

Flag this post as spam?

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


  • Lesley 284 posts 143 karma points
    Jul 25, 2009 @ 21:27
    Lesley
    0

    umbraco.library: TruncateString

    Is it possible to pass a variable containing html to the third variable in TruncateString? This is what I have:

    <xsl:variable name="continueLink">
        <a href="{umbraco.library:NiceUrl(@id)}">... read more...</a>
    </xsl:variable>

    <xsl:value-of select="umbraco.library:TruncateString(umbraco.library:StripHtml(data [@alias = 'bodyText']), $MaxNoChars, $continueLink) />

    ... but this is only outputting "... read more..." (no link).

     

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Jul 25, 2009 @ 22:00
    Jan Skovgaard
    0

    I guess it should be possible.

    Are you sure the link is not being outputted and just not being rendered because the href attribute might not get any value? What does your HTML source look like after this function has been run?

    /Jan

  • Lesley 284 posts 143 karma points
    Jul 25, 2009 @ 23:07
    Lesley
    0

    It's just plain text. I did a test without the href like

    <xsl:variable name="continueLink2">
        <b>... read more...</b>
    </xsl:variable>

    ... and the <b> tags are not output either.

  • Thomas Höhler 1237 posts 1709 karma points MVP
    Jul 25, 2009 @ 23:15
    Thomas Höhler
    0

    Try to cast into a string like

    <xsl:value-of
    select="umbraco.library:TruncateString(umbraco.library:StripHtml(data
    [@alias = 'bodyText']), $MaxNoChars, string($continueLink)) />

    I think the problem is that you have valid xml in the variable and it gives the xmlnode to the function

    Thomas

  • Lesley 284 posts 143 karma points
    Jul 26, 2009 @ 00:03
    Lesley
    0

    Hmm... No luck with string($continueLink) I'm afraid. Should have said earlier I already tried that, sorry.

    I also tried umbraco.library:HtmlEncode($continueLink)
    ... and umbraco.library:HtmlEncode(string($continueLink))

    All giving same plain text result. Is this a bug do you think?

    Rather than spend any more frustrating time on this, I think I will make a work-around such as:

    if lengthOfPost > $MaxNoChars then
      truncate post to MaxNoChars
      concat the link
    else
      just display post

    If anyone can get the thing to work like I wanted in the first place, or any other suggestions, I'd like to hear about it.

    Cheers all!

  • Daniel.S 50 posts 135 karma points
    Jul 26, 2009 @ 03:48
    Daniel.S
    0

    Just remember that the TruncateString method includes the length of the 3rd param in the overall trimmed length.

    i.e.

    <xsl:value-of select="umbraco.library:TruncateString('12345678',5,'...')"/>

    Yields:

    12...

    I don't understand why you're trying to do it this way. I just truncate the "body text" by itself, and then show the "read more" link independantly.

    You could always set a variable that tells you if the body text is more than $MaxNoChars, and then test against that when choosing to truncate the text and display the link.

  • Morten Bock 1867 posts 2140 karma points MVP 2x admin c-trib
    Jul 26, 2009 @ 10:52
    Morten Bock
    0

    You may need to escape your html, so it is not seen as a xml node:

    <xsl:variable name="continueLink2">
        &lt;b&gt;... read more...&lt;/b&gt;
    </xsl:variable>

    Or try something like:

    <xsl:variable name="continueLink2" select="'&lt;b&gt;... read more...&lt;/b&gt;'" />

     

  • Douglas Robar 3570 posts 4711 karma points MVP ∞ admin c-trib
    Jul 26, 2009 @ 17:17
    Douglas Robar
    3

    Do be careful with truncate string if that string has html/xml in it. You could end up truncating right in the middle of a tag. Imagine if your truncated text included "<b>blah" and missed the closing </b> tag.

    To add the truncate text I'd probably use the concat() function or simply output it directly with

    <xsl:text disable-output-escaping="yes">&lt;b&gt;... read more...&lt;/b&gt;</xsl:text>

     

     

    I actually wrote an xslt macro for an umbraco v3 site some time ago to trim some bodyText for use in showing blurbs next to links. Perhaps my approach would be an alternative to your?

    My template code looked like:

    <p>
        <xsl:call-template name="BlurbText">
            <xsl:with-param name="source" select="current()/data[@alias='bodyText']" />
        </xsl:call-template>
        <a href="{umbraco.library:NiceUrl(@id)}" class="More">more</a>
    </p>

    The "BlurbText" macro contains the following. You'll notice that in my case I strip the html out of the text before trimming to length (hard-coded at 40 characters but this could be passed in as a variable). In my use I only wanted the actual text even if there were html markup in the item I was displaying.

        <!-- ================================================================================== -->

        <xsl:template name="BlurbText">
            <xsl:param name="source"/>
            <xsl:if test="string($source/data [@alias='blurb']) != ''">
                <xsl:call-template name="FirstWords">
                    <xsl:with-param name="words" select="40"/>
                    <xsl:with-param name="text" select="umbraco.library:StripHtml($source/data [@alias='blurb'])"/>
                </xsl:call-template>
            </xsl:if>
            <xsl:if test="string($source/data [@alias='blurb'])  = ''">
                <xsl:call-template name="FirstWords">
                    <xsl:with-param name="words" select="40"/>
                    <xsl:with-param name="text" select="umbraco.library:StripHtml($source/data [@alias='bodyTextPrimary'])"/>
                </xsl:call-template>
            </xsl:if>

        </xsl:template>

        <!-- ================================================================================== -->

        <xsl:template name="FirstWords">
            <xsl:param name="words"/>
            <xsl:param name="text"/>

            <xsl:if test="$words &gt;= 1">
                <xsl:if test="umbraco.library:LastIndexOf($text, ' ') &gt; 0">
                    <xsl:value-of select="substring-before($text, ' ')" disable-output-escaping="yes"/><xsl:text> </xsl:text>
                </xsl:if>
                <xsl:if test="umbraco.library:LastIndexOf($text, ' ') &lt;= 0">
                    <xsl:value-of select="$text" disable-output-escaping="yes"/><xsl:text> </xsl:text>
                </xsl:if>

                <xsl:call-template name="FirstWords">
                    <xsl:with-param name="words" select="$words - 1"/>
                    <xsl:with-param name="text" select="substring-after($text, ' ')"/>
                </xsl:call-template>
            </xsl:if>

            <xsl:if test="$words = 1
                    and umbraco.library:LastIndexOf($text, ' ') &gt; 0">
                <xsl:text>... </xsl:text>
            </xsl:if>

        </xsl:template>


        <!-- ================================================================================== -->

    Hope this helps or gives you some inspiration!

    cheers,
    doug.

  • Douglas Robar 3570 posts 4711 karma points MVP ∞ admin c-trib
    Jul 26, 2009 @ 17:20
    Douglas Robar
    0

    Sorry, I mis-spoke... The "more" link was in the macro (not template as I said above) and it then called the helper template to get the blurb text.

    cheers,
    doug.

  • Petr Snobelt 923 posts 1535 karma points
    Jul 26, 2009 @ 21:03
    Petr Snobelt
    0

    I'don't read whole discution, but if you need html output use xsl:copy-of instaed of xsl:value-of

Please Sign in or register to post replies

Write your reply to:

Draft