Copied to clipboard

Flag this post as spam?

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


  • Toni Becker 146 posts 425 karma points
    Jun 18, 2011 @ 10:07
    Toni Becker
    1

    New Snippet *** Truncate Strings (not removing HTML tags)

    So here comes new snippet for our RAZOR lovers. I needed this for a client project. In the past i turned my back away from xslt. After testing with truncate helper and trim etc. i developed this solution, based on the MVC3 hints on ASP.NET.
    If somebody would suggest improvements, post it in here.
    Have a nice day.

    CODE:

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @helper Truncate(string input, int length)
    {
    if (input.Length <= length) {
    @Html.Raw(input)
    } else {
    @Html.Raw(umbraco.library.RemoveFirstParagraphTag(input.Substring(0, length)))<text>...</text>
    }
    }

    @{
    //GIVE ME THE ROOTNODE
    var start = Model.AncestorOrSelf();
    }
    //CRAWL THROUGH MY NODES AND CHOSE ONLY THE ONES WITH DOCTYPE NEWS
    @foreach(var article in start.Descendants("News"))
    {
    var excerpt = article.newsText.ToString();
    <a href="@article.Url" class="news_article">
    <strong class="news_title">@artikel.title</strong>
    //YOU CAN COMBINE THIS WITH A MACRO PARAMETER TO DEFINE THE NUMBER AFTER CUTTING THE TEXT
    <span class="news_short"><p>@Truncate(excerpt, 300)</p></span>
    <span class="date">@artikel.date</span>
    </a>
    }
  • Toni Becker 146 posts 425 karma points
    Jun 19, 2011 @ 16:10
    Toni Becker
    1

    UPDATE:


    reason: the truncate is stripping of html tags at the end, because it's formatted to string and html.encoded.

    @inherits umbraco.MacroEngines.DynamicNodeContext
    @helperTruncate(string input,int length)
    {
       
    if(input.Length<= length){
           
    @Html.Raw(input)
       
    }else{
           
    @Html.Raw(umbraco.library.RemoveFirstParagraphTag(input.Substring(0, length)))<text>...</text>
       
    }
    }

    @{
       
    //GIVE ME THE ROOTNODE
       
    var start =Model.AncestorOrSelf();
    }
    //CRAWL THROUGH MY NODES AND CHOSE ONLY THE ONES WITH DOCTYPE NEWS
    @foreach(var article in start.Descendants("News"))
       
    {  
           
    var excerpt = article.newsText.ToString();
           
    <a href="@article.Url"class="news_article">
           
    <strong class="news_title">@artikel.title</strong>
            /
    /YOU CAN COMBINE THIS WITH A MACRO PARAMETER TO DEFINE THE NUMBER AFTER CUTTING THE TEXT
           
    <span class="news_short"><p><text>@Truncate(excerpt,300)</text></p></span>
           
    <span class="date">@artikel.date</span>
            </
    a>
       
    }

    So just wrap the line with the @Truncate inside a <text></text> element. So it's ignoring the inside tags.

  • 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