Copied to clipboard

Flag this post as spam?

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


  • Inmedia 124 posts 176 karma points
    Sep 17, 2014 @ 10:21
    Inmedia
    0

    Display only first 100 characters from property

    Hi guys

    I have a problem I hope someone can help me with. 

    I'm trying to create a razor script that displays only the first 100 characters of a property (Textbox multiple). I would think it is an easy thing to achive, but I just can't figure out how to do it.

    Can someone please help me out?

    I know how to display all of the text, as shown below, but what do I need to add, to take only the first 100 characters?

    <p>@page.brdtekst</p>

     

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Sep 17, 2014 @ 10:23
    Jeavon Leopold
    0

    Hi, Truncate is what you need:

    <p>@Umbraco.Truncate(page.brdtekst.ToString(),100)</p>
    

    Jeavon

  • Inmedia 124 posts 176 karma points
    Sep 17, 2014 @ 10:32
    Inmedia
    0

    Hi Jeavon

    Im getting following error, when using the snippet you posted above:

    Error occured

    d:\web\pilotskolen.com\www\MacroScripts\635465466176816766_BlogRoll.cshtml(31): error CS0234: The type or namespace name 'Truncate' does not exist in the namespace 'Umbraco' (are you missing an assembly reference?)

     

    Do you have any idea what's wrong?

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Sep 17, 2014 @ 10:34
    Jeavon Leopold
    0

    What version of Umbraco are you using?

  • Inmedia 124 posts 176 karma points
    Sep 17, 2014 @ 10:53
    Inmedia
    0

    I'm using version 6.1.6

  • Inmedia 124 posts 176 karma points
    Sep 17, 2014 @ 10:54
    Inmedia
    0

    Maybe I should post the entire Razor script too:

    @inherits umbraco.MacroEngines.DynamicNodeContext

    @{    

    @*Build a query and return the visible items *@

        var selection= Model.DescendantsOrSelf("BlogPost").Where("Visible");

    }

    @*Determine if there are any nodes in the selection, then render list *@

    @if(selection.Any()){

       <ul>

         @foreach(var page in selection){                

            <li>

     <h3><a href="@page.Url">@page.Name</a></h3>

    <p class="lead">@page.teaser</p>

    <p>@Umbraco.Truncate(page.brdtekst.ToString(),100)</p>

    <a class="button" href="@page.Url">Læs indlæg</a>

    </li> 

         }                 

       </ul>            

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Sep 17, 2014 @ 10:56
    Jeavon Leopold
    0

    Ok, is there a reason you are using a legacy MacroScript rather than the current Partial View Macro?

  • Inmedia 124 posts 176 karma points
    Sep 17, 2014 @ 10:59
    Inmedia
    0

    Well, I don't know what the difference is, to be honest... Im pretty new to Razor.

    So no, I guess there is no reason legacy MacroScript.

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Sep 17, 2014 @ 11:00
    Jeavon Leopold
    0

    Ok cool, are you using MVC templates or WebForms?

  • Inmedia 124 posts 176 karma points
    Sep 17, 2014 @ 11:03
    Inmedia
    0

    Ehm.... Again, I actually don't know :)   I just use the templates as they are being created, when I create a new doctype. Can you see which I am using from this first line in the templates:

    <%@ Master Language="C#" MasterPageFile="~/masterpages/Master.master" AutoEventWireup="true" %>

  • Inmedia 124 posts 176 karma points
    Sep 17, 2014 @ 11:20
    Inmedia
    0

    I solved it with a helper, like this:
    Only problem is that I can't strip the

    tag 

     

    @inherits umbraco.MacroEngines.DynamicNodeContext

    @helper Truncate(string input, int length)

    {

        if (input.Length

            @input

        } else {

            @input.Substring(0, length)...

        }

    }

     

    @{    

    @*Build a query and return the visible items *@

        var selection= Model.DescendantsOrSelf("BlogPost").Where("Visible");

    }

     

    @if(selection.Any()){

       

         @foreach(var page in selection){    

    var excerpt = page.brdtekst.ToString();

    var bodyText = page.brdtekst.ToString();

           

     

    @page.Name

    @page.teaser

     

     @Truncate(bodyText, 150); 

    Læs indlæg

     

         }                 

                   

    }

  • Inmedia 124 posts 176 karma points
    Sep 17, 2014 @ 11:23
    Inmedia
    0

    Got it... ! :)  heres the working script:

     

    @helper Truncate(string input, int length)

    {

        if(input.Length<=length){

            @Html.Raw(input)

        }else{

            @Html.Raw(input.Substring(0,length))<text>...</text>

        }

    }

    @{    

    @*Build a query and return the visible items *@

        var selection= Model.DescendantsOrSelf("BlogPost").Where("Visible");

    }

    @if(selection.Any()){

       <ul>

         @foreach(var page in selection){    

    var excerpt = page.brdtekst.ToString();

    var bodyText = page.brdtekst.ToString();

            <li>

     <h3><a href="@page.Url">@page.Name</a></h3>

    <p class="lead">@page.teaser</p>

     

     @Truncate(bodyText, 150); @* ## YOU CAN COMBINE THIS WITH A MAC-PARAM..*@

    <a class="button" href="@page.Url">Læs indlæg</a>

    </li> 

         }                 

       </ul>             

    }

  • Jeavon Leopold 3072 posts 13628 karma points MVP 10x admin c-trib
    Sep 17, 2014 @ 11:46
    Jeavon Leopold
    0

    Ok, you're using WebForms templates then. I would highly recommend that you switch to Partial View Macros and then the @Umbraco helper will be there for you.

    To switch this script, you would only need to change Model to CurrentPage. e.g.

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    @{    
    
    @*Build a query and return the visible items *@
    
        var selection= CurrentPage.DescendantsOrSelf("BlogPost").Where("Visible");
    
    }
    
    @*Determine if there are any nodes in the selection, then render list *@
    
    @if(selection.Any()){
    
       <ul>
    
         @foreach(var page in selection){                
    
            <li>
    
     <h3><a href="@page.Url">@page.Name</a></h3>
    
    <p class="lead">@page.teaser</p>
    
    <p>@Umbraco.Truncate(page.brdtekst.ToString(),100)</p>
    
    <a class="button" href="@page.Url">Læs indlæg</a>
    
    </li> 
    
         }                 
    
       </ul>            
    
    } 
    
Please Sign in or register to post replies

Write your reply to:

Draft