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
    Apr 08, 2011 @ 14:02
    Toni Becker
    1

    New Snippet - Truncate your string with Razor helpers

    Here's  what i have done - using a helper to trim my string.

    Here's the code:

    @helper Truncate(string input, int length)
    {
        if (input.Length <= length) {
            @input
        } else {
            @input.Substring(0, length)<text>...</text>
        }
    }

    @foreach(dynamic article in Model.Children)
    {
     @article.siteTitle
     var bodyText = article.siteContent.ToString();
     @Truncate(bodyText, 20); @* ## YOU CAN COMBINE THIS WITH A MAC-PARAM..*@
    }

    Have fun dudes :)

  • Chizzl 36 posts 107 karma points
    Jul 27, 2011 @ 12:27
    Chizzl
    0

    If you add Html.Raw, then it also works when your string contains HTML tags. Bumped into this once... :)

    @helperTruncate(stringinput,intlength)
    {
        if(input.Length<=length){
            @Html.Raw(input)
        }else{
            @Html.Raw(input.Substring(0,length))<text>...</text>
        }
    }
Please Sign in or register to post replies

Write your reply to:

Draft