Developer forum

Forum » Templates » Fixed style

Reply
Hi!

How do you do to completely ignore the formatting of the text in a paragraph? I just want to get the text the user typed and use the fixed formatting from the css-file, not the formatting from the user.

Best regards,

Per

Replies

 
Nicolai Høeg Pedersen
Reply
Using CSS - overrule everything.

From management center you can remove editing options from the user - so they cannot use colors, fonts etc.
 
Reply
Hi!

But is it possible to get the user input as clean text, no formatting at all, just the text.

Like <!--@Ecom.Product.ShortDescription.Clean-->

Best regards,

Per
 
Nicolai Høeg Pedersen
Reply
No - not possible...
 
Reply
Hi Per,

You could go wild and use a NotificationSubscriber that responds to OnBeforeRender. Inside the OnNotify method, you can grab the HTML, clean it, and reassign it. Something like the following would give you clean text without any HTML:

using System;
using System.Text.RegularExpressions;
using Dynamicweb.Extensibility;

namespace Dynamicweb.Samples.Lib
{
static class Helpers
{
public static string StripHtml(this string html)
{
if (html == nullthrow new ArgumentNullException("html");
var re = new Regex(@"<[\/!A-z]+(?:.*?(?:=\s?(?:(""|')[^\1]*\1|[^\s>]*))?)+(?:>|(<))");
return re.Replace(html, @"${2}");
}
}

[Subscribe(Dynamicweb.Notifications.Standard.Paragraph.OnBeforeRender)]
public class OnBeforeRenderNotificationSubscriber : NotificationSubscriber
{
public override void OnNotify(string notification, NotificationArgs args)
{
Dynamicweb.Notifications.Standard.Paragraph.OnBeforeRenderArgs myArgs =  args as Dynamicweb.Notifications.Standard.Paragraph.OnBeforeRenderArgs;
myArgs.Paragraph.Paragraph.ParagraphText = myArgs.Paragraph.Paragraph.ParagraphText.StripHtml();
}
}
} Note: this strips away ALL HTML, so may want to alter StripHtml to only remove styling related stuff. Also note: this fires for each and every request, so may want to build in some if stataements that makes this code only run when you really need it. Hope this helps, Imar
 
Reply
Is it possible to do this using regular expressions in XSLT?

Best regards,

Per
 
Reply
Don't know. Me and XSLT don't mix very well.... ;-)

BTW: you could also create a tag and only render the stripped content when the tag exists:

if (myArgs.template.TagExists("Custom:ParagraphTextClean"))
{
myArgs.template.SetTemplateValue("Custom:ParagraphTextClean", myArgs.Paragraph.Paragraph.ParagraphText.StripHtml());
}
else
{
myArgs.template.SetTemplateValue("Custom:ParagraphTextClean"String.Empty);
}
Now, stripping only takes place when your template file contains the tag: Custom:ParagraphTextClean. Not sure how future proof this is though. The template property is the "old" template object and not a TemplateV2 instance. It works fine like this, but it might break in the future. Hope this helps, Imar








 

You must be logged in to post in the forum