Copied to clipboard

Flag this post as spam?

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


  • Mohammad Javed 64 posts 373 karma points
    Nov 06, 2015 @ 10:36
    Mohammad Javed
    0

    Can't seem to render text in foreach loop

    Hi,

    I am trying to render the blog content and then trim this to a certain length, however i can't seem to fathom out on how to do this,

    I've tried .HasValue and that doesn't seem to work either;

    @inherits Umbraco.Web.Macros.PartialViewMacroPage
    
    @{
        var pageSize = 5;
        var page = 1; int.TryParse(Request.QueryString["page"], out page);
        var items = Model.Content.Children().Where(x => x.IsDocumentType("Article")).OrderByDescending(x => x.CreateDate);
        var totalPages = (int)Math.Ceiling((double)items.Count() / (double)pageSize);
    
        if (page > totalPages)
        {
            page = totalPages;
        }
        else if (page < 1)
        {
            page = 1;
        }
    
    }
    
    @helper Truncate(string input, int length)
    {
        if (input.Length <= length) {
            @input
        } else {
            @input.Substring(0, length)<text>...</text>
        }
    }
    
    @foreach (var blogPost in items.Skip((page - 1) * pageSize).Take(pageSize))
    {
        <!--Article-->
        <article class="post-preview blogItem large-12 columns noPaddingLeft" id="[email protected]">
    
            @{
                if (blogPost.HasValue("blogThumbnail"))
                {
                    var mediaItem = Umbraco.TypedMedia(blogPost.GetPropertyValue("blogThumbnail"));               
    
                    <div class="blogThumbnail left large-4 columns noPaddingLeft">
                        <a href="@blogPost.Url" title="@blogPost.Name">
                            <img class="img-thumbnail full" src="@mediaItem.GetPropertyValue("umbracoFile")" alt="@mediaItem.GetPropertyValue("Name")" />
                        </a>
                    </div>
                }
                else
                {
                    <div class="blogThumbnail left large-4 columns noPaddingLeft">
                        <a href="@blogPost.Url" title="@blogPost.Name">
                            <img src="http://placehold.it/800x800" alt="placeholder">
                        </a>
                    </div>
                }
            }
    
            <div class="blogInfo large-8 columns">
    
    
                <div class="blogPostInfo">
                    <span class="date">
                        <i class="fa fa-calendar"></i>
                        @blogPost.CreateDate.ToString("dd-MM-yyyy")
                    </span>
                    <span class="author">
                        <i class="fa fa-pencil"></i>
                            Damian Hughes
                    </span>
                    <span class="comments">
                        <i class="fa fa-comment"></i>
                            <a href="@blogPost.Url#disqus_thread">0 comments</a>
                    </span>                                       
                </div>
    
                <!-- Problem is here -->
                <div class="blogArticleCont">
                    @{
                        if (blogPost.HasValue("blogContent"))
                        {
                            @Umbraco.TypedContent(blogPost.GetPropertyValue("blogContent"))
                        }
                        else
                        {
    
                            <p>Hello</p>
                        }    
                    }            
                </div>
    
                <div class="clearfix"></div>
    
    
    
            </div>
    
    
        </article>
        <hr /> 
    }
    
    @if (totalPages > 1)
    {
        <ul class="pagination">
            @if (page > 1)
            {
                <li><a href="?page=@(page-1)">Prev</a></li>
            }
            @for (int p = 1; p < totalPages + 1; p++)
            {
                var active = (p == page) ? "active" : string.Empty;
                <li class="@active">
                    <a href="?page=@p">@p</a>
                </li>
            }
            @if (page < items.Count())
            {
                <li><a href="?page=@(page+1)">Next</a></li>
            }
        </ul>
    }
    

    UPDATE:

    Aha! Gotchya! :-)

    I had did to use @Html.Raw(blogPost.GetPropertyValue("blogContent"))

    I keep on forgetting to use "GetPropertyValue" - my downfall! Agghhh!

  • Marc Goodson 2155 posts 14406 karma points MVP 9x c-trib
    Nov 09, 2015 @ 11:14
    Marc Goodson
    101

    Hi Mohammad

    Also there are a couple of Umbraco helper methods that can help with the truncate...

    Umbraco.Truncate("text to truncate",25)
    

    would strip to 25 characters long,

    and also if that might cause a truncation in the middle of some html, you can strip the html first with

    Umbraco.StripHtml("<p>htmlText</p>")
    

    So you could have

    @Umbraco.Truncate(Umbraco.StripHtml(blogPost.GetPropertyValue("blogContent")), 50)
    

    regards

    Marc

  • Mohammad Javed 64 posts 373 karma points
    Nov 09, 2015 @ 16:32
    Mohammad Javed
    0

    Hello Marc,

    I prefer the last helper method, more understandable and neater :-)

    I've used it and the result is brilliant. Love it, going to trim my text using the last helper method listed.

    Thanks

    /Javed

Please Sign in or register to post replies

Write your reply to:

Draft