Reputation: 79
Anyone here that can help me? I have the following code:
@inherits umbraco.MacroEngines.DynamicNodeContext
@{
var node = @Model.NodeById(1257);
}
<div class="Top10">
<h1>Newest</h1>
@foreach (var article in node.Descendants().Where("Visible && (NodeTypeAlias = \"Article\" || NodeTypeAlias = \"sergrein\" || NodeTypeAlias = \"solomyndagrein\")").OrderBy("createDate desc").Take(10))
{
<a href="@article.Url"><h2>@article.createDate.ToString("dd/MM") | @article.title</h2></a>
}
</div>
What I want is: if @article.title is longer than e.g. 10 characters, it needs to return the 10 characters followed by ... (for example: "this_is_a_..."). If the @article.title is shorter than 10 characters, it can just show the full title length. How can this truncating be done?
Upvotes: 6
Views: 36580
Reputation: 1458
this shall help,
@{
if(article.title.ToString().Length > 10)
{
article.title = article.title.Substring(0,10) + " ..."; //the format you desire
}
else
{
article.title; // default
}
}
Upvotes: 1
Reputation: 3194
Try this
@(article.title.Length > 10 ? (article.title.Substring(0,10) + " ...") : article.title)
Upvotes: 4
Reputation: 65476
Normally I'd say do this in your Model, but it looks like you're using Umbraco's model.
So you could just do:
@inherits umbraco.MacroEngines.DynamicNodeContext
@{
var node = @Model.NodeById(1257);
}
<div class="Top10">
<h1>Newest</h1>
@foreach (var article in node.Descendants().Where("Visible && (NodeTypeAlias = \"Article\" || NodeTypeAlias = \"sergrein\" || NodeTypeAlias = \"solomyndagrein\")").OrderBy("createDate desc").Take(10))
{
{
var title = article.title;
if (title.Length > 10)
title = title.Substring(0,10) + "...";
}
<a href="@article.Url"><h2>@article.createDate.ToString("dd/MMM") | @title</h2></a>
}
</div>
Upvotes: 1