[Question] Short question content
Opened this issue · 0 comments
micswe commented
Hello,
i tred to split a string and work with a each loop, but i unable to return the array. You can see my source test code for this.
public class HandlebarsHelpers
{
public static void RegisterHelpers()
{
Handlebars.RegisterHelper("stringToArray", (writer, context, parameters) =>
{
if (parameters.Length != 1 || parameters[0] == null)
{
throw new ArgumentException("stringToArray helper requires exactly one string parameter.");
}
string inputString = parameters[0].ToString();
var resultArray = inputString.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
writer.WriteSafeString("[");
for (int i = 0; i < resultArray.Length; i++)
{
writer.WriteSafeString($"\"{resultArray[i].Trim()}\"");
if (i < resultArray.Length - 1)
{
writer.WriteSafeString(",");
}
}
writer.WriteSafeString("]");
});
}
}
public static class hadlebartest
{
public static void handeltest()
{
// Registrierung der Helper
HandlebarsHelpers.RegisterHelpers();
// Beispiel-Handlebars-Template
var source = @"
<ul>
{{#each (stringToArray inputString)}}
<li>{{this}}</li>
{{/each}}
</ul>";
var template = Handlebars.Compile(source);
// Kontext-Daten
var data = new
{
inputString = "apple, banana, cherry"
};
// Rendern des Templates mit den Daten
var result = template(data);
Console.WriteLine(result);
// The result shoud be
/*
<ul>
<li>apple</li>
<li>banana</li>
<li>cherry</li>
</ul>
*/
// My Result is
//< ul >
// < li > 27 </ li >
//</ ul >
//its the length of the string
}
}
I use .net 8 with Handlebars.Net 2.1.6