Press Ctrl / CMD + C to copy this to your clipboard.
This post will be reported to the moderators as potential spam to be looked at
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"))
@Html.Raw(blogPost.GetPropertyValue("blogContent"))
I keep on forgetting to use "GetPropertyValue" - my downfall! Agghhh!
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
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
is working on a reply...
Write your reply to:
Upload image
Image will be uploaded when post is submitted
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;
UPDATE:
Aha! Gotchya! :-)
I had did to use
@Html.Raw(blogPost.GetPropertyValue("blogContent"))
I keep on forgetting to use "GetPropertyValue" - my downfall! Agghhh!
Hi Mohammad
Also there are a couple of Umbraco helper methods that can help with the truncate...
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
So you could have
regards
Marc
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
is working on a reply...