Get content block without HTML - c#

Is there a built-in Sitefinity method for displaying blog post content without the HTML? NOT the standard standalone C# methodology, but a built-in method in Sitefinity so I can avoid duplicating functionality.
A built in method in Sitefinity, not a generic C# solution.
I'd like to show a summary from my content rather than the separate summary field. I'm in my custom blog list .cshtml file, doing this to retrieve content:
Html.Raw(item.Fields.Content).ToString()
Which gives me the content, but I need to display with without the HTML tags a user may have added.

If you refer using Telerik.Sitefinity.Utilities you will get an extension method StripHtmlTags for the string type.
This is a public extension method and you should not have problems using it.

I see there are a few classes in Sitefinity that do HTML stripping:
public static HtmlStripper in Telerik.Sitefinity.Modules.Newsletters.Composition namespace.
and
internal static class HtmlStripper in Telerik.Sitefinity.Utilities.HtmlParsing namespace, which seems to be the one you need, because it has a StripHtml method, but you cannot use it as it is internal.
I agree with #Zohar that in this particular case you should just use a standard C# approach - you cannot go wrong with it.

Related

How to create RazorEngineHost for RazorTemplateEngin in non MVC project

I'm using RazorEngine in an Console application.
I want to initialise RazorEngineHost and pass into RazorTemplateEngin. However as I can see in MSDN documentations it says RazorEngineHost "is not intended to be used directly from your code".RazorEngineHost
So what is the best way of creating RazorTemplateEngin?
This question quite different from How to use Razor View Engine in a console application?> I want to use RazorTemplateEngin becuse I have template not string.
If you have a look at RazorTemplateEngine on MSDN you can see that the documentation for that also says "not intended to be used directly from your code", that doesn't mean that you can't. If you want to use an instance of RazorTemplateEngineyou have to pass in a RazorEngineHost instance. Have a look at this blog post for usage: Leveraging Razor Templates Outside of ASP.NET
var language = new CSharpRazorCodeLanguage();
var host = new RazorEngineHost(language) {
DefaultBaseClass = "OrderInfoTemplateBase",
DefaultClassName = "OrderInfoTemplate",
DefaultNamespace = "CompiledRazorTemplates",
};
host.NamespaceImports.Add("System");
To begin, the RazorEngineHost’s constructor accepts a RazorCodeLanguage specifying the target template’s code language. This example produces a host that can parse Razor templates written using C#. To support templates written in Visual Basic, supply a VBRazorCodeLanguage instance instead. The additional initializer properties instruct the code generator to emit code with a particular class name, deriving from a custom template base class, and residing in a particular namespace. Finally, add the System namespace to the list of imported namespaces required for the generated class to compile just as you would import a namespace in a normal, hand-written class.

T4 Text Templates Pass object to included Template

I am using Preprocessed Text Templates (.tt) . How can I pass object to another included Preprocessed Text Template? I checked the documentation but didn't find any sample to do that.
Thanks
If you look at the generated code from your preprocessed template, you'll see that it is just a partial class. A great way to pass in data is simply to add another partial of the class, and provide a member and Getter/Setter pair for it. If you then include some further code in that preprocessed template, it will contribute to the same class, so your added member will still be available. If on the other hand, your included code is just class feature clocks (<#+ #>), then you'll likely be defining methods that you can call, in which case you can simply add your data to pass as an extra parameter to those methods.

What does an #functions code block in a razor file do, and when (if ever) should I use it?

Whilst looking at a theme I downloaded from the Orchard CMS gallery, I noticed that a Layout.cshtml file had this block of code at the top of the file:
#functions {
// To support the layout classifaction below. Implementing as a razor function because we can, could otherwise be a Func<string[], string, string> in the code block following.
string CalcuClassify(string[] zoneNames, string classNamePrefix)
{
var zoneCounter = 0;
var zoneNumsFilled = string.Join("", zoneNames.Select(zoneName => { ++zoneCounter; return Model[zoneName] != null ? zoneCounter.ToString() : ""; }).ToArray());
return HasText(zoneNumsFilled) ? classNamePrefix + zoneNumsFilled : "";
}
}
I know what the declared function does (calculates which zones are populated in order to return the width of each column), my question is- what is the correct use of the #function block, and when should I ever use it?
The #functions block lets you define utility functions directly in the view, rather than adding them as extensions to the #Html helper or letting the controller know about display properties. You'd want to use it when you can meet these conditions:
The functionality is tied closely to the view and is not generally useful elsewhere (such as "How wide do I make my columns").
The functionality is more than a simple if statement, and/or is used in multiple places in your view.
Everything that the function needs to determine it's logic already exists in the Model for the view.
If you fail the first one, add it as a #Html helper.
If you fail the second one, just inline it.
If you fail the third one, you should do the calculation in your controller and pass the result as part of the model.
Others have explained what #functions does so I won't rehash that. But I would like to add this:
If your view is typed to a viewmodel, I think a viable option would be to move this logic into the viewmodel to avoid cluttering your markup with too much code. Otherwise your views start to look more and more like classic ASP and I don't think anybody wants that.
I don't think there's anything wrong with using #functions or #helper in your view, but once you get beyond a couple of methods in your view, or even if the function is somewhat complicated, it might be worth refactoring to the viewmodel if at all possible. If it's code that can be reused, it may be a good idea to to pull it out into a helper class or an extension to the HtmlHelper class. One thing that is a bummer is realizing you just rewrote a piece of code that already existed because you didn't know it was hidden away in some arbitrary view.
From msdn blogs, #functions block is to let you wrap up reusable code, like the methods and properties
In this particular case, the people who have created the theme you are using probably were trying to keep it as a simple theme (only views, css and images).
If you need to write some code for a theme for Orchard, you have to turn to a module (as stated here: http://docs.orchardproject.net/Documentation/Anatomy-of-a-theme) unless you write this code in the view.
I am not sure it is worth the time to switch from a theme to a module only to get the size of a column.

ASP.NET MVC: Get lowercase links (instead of Camel Case)

All my dynamically generated action links etc. are creating links like /Account/Setup. It looks strange.
I'd like all my links to be lowercase (meaning /account/setup). Any way to do this?
There is simpler solution in .NET Framework 4.5, a new property RouteCollection.LowercaseUrls, here's an example
Take a look at http://goneale.com/2008/12/19/lowercase-route-urls-in-aspnet-mvc/. You can find more information in another stackoverflow at How can I have lowercase routes in ASP.NET MVC?.
The other posts thus far have not tackled the scenario where you navigate to the root of your web directory. If you have a mapping that directs to the HomeController Index action, you would like the following URL to appear:
mysite/home/ or even mysite/home/index
No amount of Html helper function usage will change the fact that, by default, the following will be shown in the browser location bar:
mysite/Home or mysite/Home/Index
Write an extension method for Html:
public static class MyHtmlExtensions
{
public static string LowerActionLink(this HtmlHelper htmlHelper,someargs)
{
return String.ToLowerInvariant(htmlHelper.ActionLink(someArgs));
}
}
Then use Html.LowerActionLink instead of Html.ActionLink

Create custom tag

In my views I verry often have to write
<%=Model.T9n.TranslateById("someId");%>
Is there some way to make this more compact/readable. A custom tag maybe?
<asp:T9n>someId</asp:T9n>
or better
<t9n: someId />
Note
There are several (business) reasons why I can't use the default T9n tools in visual studio so that's why I've rolled my own.
I don't know anything about creating custom tags in MVC...
You could create your own custom HtmlHelper extension method. E.g.
<%=Html.T9n("someId");%>
HTHs,
Charles
Yes you can create a custom control and get something like <prefix:t9n translationID="someID" runat="server"/>
Just Google for web custom control and you'll get tons of hits.

Categories