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?
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?)
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:
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>
}
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>
Hi, Truncate is what you need:
Jeavon
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?
What version of Umbraco are you using?
I'm using version 6.1.6
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>
}
Ok, is there a reason you are using a legacy MacroScript rather than the current Partial View Macro?
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.
Ok cool, are you using MVC templates or WebForms?
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" %>
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
}
}
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>
}
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.
is working on a reply...