Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] Short question content #587

Open
micswe opened this issue Jul 26, 2024 · 0 comments
Open

[Question] Short question content #587

micswe opened this issue Jul 26, 2024 · 0 comments
Labels

Comments

@micswe
Copy link

micswe commented Jul 26, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant