Using Handlebars.Net, I'd like to create a HandlebarsHelper that will replace carriage returns and newlines with <br>
tags. It should look something like this:
string pattern = @"/(\r\n|\n|\r)/gm";
string replacement = "<br>";
Regex rgx = new Regex(pattern);
Handlebars.RegisterHelper("link_to", (string text) =>
{
text = rgx.Replace(text, replacement);
});
The compiler (or resharper) is telling me that it can't tell if I'm trying to use HandlebarsBlockHelper or HandlebarsHelper, and I'm missing arguments in either case.
It ended up looking like this with a little help from the C# Regex class:
var newlineRegx = new Regex("(\\r\\n|\\n|\\r)",RegexOptions.Multiline);
Handlebars.RegisterHelper("handleNewLines", (output, context, arguments) =>
{
var str = newlineRegx.Replace((string)arguments[0], "<br>");
output.Write(str);
});
To answer my questions:
HandleBarsBlockHelper provides a mechanism for invoking a helper with a block of the template. Block helpers can then invoke that block zero or more times with any context it chooses. Check out the description for Helpers for more info (at the bottom you'll see a button labeled "Learn More: Block Helpers").
To better understand the argument object list, it'll help to see how I used this helper in my HTML template:
<div>
<p>{{{handleNewLines StringVariable}}}</p>
</div>
Where "StringVariable" is a member of the JSON object I passed into the delegate I created with Handlebars.Compile()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With