Copied to clipboard

Flag this post as spam?

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


  • Ryan Nguyen 27 posts 80 karma points
    Dec 23, 2014 @ 08:06
    Ryan Nguyen
    0

    Umbraco.Truncate strips out text between a href tag

    I'm having some issues with the Umbraco.Truncate  and or the Library.Truncate.  It seems that when using the Umbraco.Truncate, it is stripping out the text between the ahref tag.

    Original Content:

    <p><span>Below is a sample of “<a href="/media/1006/6.jpg" target="_blank" title="6.jpg">Lorem ipsum dolor sit</a>” dummy copy text often used to show font</span></p>

    Stripped Content Using (@Umbraco.Truncate(item.Content, 50)) Or (@Library.Truncate(item.Content, 50)):

    <p><span>Below is a sample of “<a href="/media/1006/6.jpg" target="_blank" title="6.jpg"></a>” d&hellip;</span></p>

    The href is still there, but the text between the tag ("Lorem ipsum dolor sit") is stripped out.

    Anyone having the same issue?

    Thanks

    Ryan

    Edit:  I'm currently on Umbraco Version 7.1.8

     

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Dec 23, 2014 @ 08:33
    Jan Skovgaard
    0

    Hi Ryan

    Could you please show the code where you're using this?

    In order to understand your issue a bit better in the proper context it would be nice to see it.

    Why do you need to truncate the text? I don't think it's the right approach if you need to make sure that the text fits inside a small box or something like that. Then I think you should consider using CSS and do something like overflow:hidden; and then give your box specific width and height. Then you can also use the text-overflow:ellipsis property.

    Looking forward to hearing from you.

    /Jan

  • Ryan Nguyen 27 posts 80 karma points
    Dec 23, 2014 @ 09:00
    Ryan Nguyen
    0

    Hey Jan,

     

    Thanks for the quick reply. 

     

    I'm trying to create a News or blog post overview. This news overview would show a list of news and display snippet of each news limiting to just 1000 characters. The idea is to just show the user a glimpse of what the topic is, and if it interest them they could read more by clicking on the read more button.  Hence, I am using the Umbraco.Truncate method to truncate the content.  Please let me know if this is the right approach.

     

    Below is my short simple source code that I mocked up.

     

    Thanks

     

    Ryan

     

     

    UmbracoTemplatePage

    @{

        // Model.Content is the current page that we're on

        // AncestorsOrSelf is all of the ancestors this page has in the tree

        // (1) means: go up to level 1 and stop looking for more ancestors when you get there

        // First() gets the first ancestor found (the home page, on level 1)

        var homePage = CurrentPage.AncestorsOrSelf(1).First();

     

        // Find all pages with document type alias umbNewsOverview

        // We do that using the plural, umbNewsOverviews (note the extra "s" in the end)

        // Then take the first one, as we know there will only be on news overview page

        var newsOverview = homePage.NewsOverviews.First();

     

        // Similar to above: find all pages with document type umbNewsItem under the news overview page

        // Then order them, first by publishDate (a property the editor can explicitly set on the news item)

        // and then by createDate, which is set by Umbraco automatically when a page gets created.

        // Finally, take the first 5 items to show in the news overview

        var newsItems = newsOverview.NewsItems.OrderBy("publishDate desc, createDate desc").Take(3);

     

    }

     

    <!-- Blog -->

    @foreach (var item in newsItems)

    {

     

        <div class="col-md-12">

            <h3>@item.Name</h3>

            <p>@item.CreateDate.ToString("MM/dd/yyy")</p>

            <img class="img-responsive thumbnail" src="@item.Image" />

       

            @Umbraco.Truncate(item.Content, 1000)

            @*<p> @item.Content</p>*@

     

        </div>

    }

     

     

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Dec 23, 2014 @ 09:05
    Jan Skovgaard
    0

    Hi Ryan

    Last time I used the truncate method it took all characters from the RTE, which means that the HTML tags are being truncated as well. And that's something you don't want.

    Perhaps you should consider adding a "Teaser" field to your news document type based on a multiple text data type. Then you should be able to use the truncate method in a predictable manner since it will now only deal with text content and no HTML.

    And then in combination with the CSS approach I mentioned above I think that will be the better option.

    Hope this helps and makes sense.

    /Jan

  • Mike Chambers 635 posts 1252 karma points c-trib
    Dec 23, 2014 @ 11:52
    Mike Chambers
    0

    If formatting of that teaser isn't important then you could use stripHtml to just give you just the plain text content before truncating to your required length.

  • jivan thapa 194 posts 681 karma points
    Dec 23, 2014 @ 13:54
    jivan thapa
    0

    As @Mike said,

    You could use

     @Umbraco.Truncate(library.StripHtml(item.Content), 1000)
    
  • Ryan Nguyen 27 posts 80 karma points
    Dec 23, 2014 @ 15:36
    Ryan Nguyen
    0

    Thanks Guys for the response.

    Unfortunately, I need to keep all the formatting, so I can't strip away the html.  I want the users to do all their editing, formatting, etc and I just take what they have and display it.

    Thanks

    Ryan

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Dec 23, 2014 @ 15:44
    Jan Skovgaard
    0

    Yeah, the approach of trimming and truncating is not the way to go about imho. You also risk cutting of text that Google might index and then sometimes the text could read "We have a lot of new products you sh..." - That's the same text that goes into the source code, which Google bot and other is reading.

    Therefore I really think you should consider using the CSS approach I mentioned above to deal with this - See this page where Chris Coyer has made a nice demo http://css-tricks.com/almanac/properties/t/text-overflow/

    Hope this helps and make my intention clearer :)

    /Jan

  • Mike Chambers 635 posts 1252 karma points c-trib
    Dec 23, 2014 @ 19:34
    Mike Chambers
    0

    Hmmm... Isn't the idea that this is a teaser and you'd want the actual news page to be the one that hits for seo? Also on a bandwidth issue it  wouldn't be long before a news section became unwieldy for mobile delivery with all that potentially substantial hidden text, and then needed progressive loading?

    I'd probably go with your teaser parameter as a separate entity as best practice then you really don't need to get involved with trimming or truncating :-) or vast hidden HTML. Writing a concise abstract for each page is actually a great way to educate your content writers for both the abstract and the actual full content.

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Dec 23, 2014 @ 19:45
    Jan Skovgaard
    0

    Yeah, a teaser field would make sense - Also what I had in mind but forgot to re-mention in my latest post where I suggest using the CSS tricks - But using a teaser field with the CSS trick mentioned above to make sure that "..." is shown if the entered teaser is for some reason too long to fit the container :)

    /Jan

Please Sign in or register to post replies

Write your reply to:

Draft