Skip to content

Custom helpers not available in Template #132

@muxa

Description

@muxa

I'm getting an error Nustache.Compilation.CompilationException : Could not find formatDateTime Timestamp format="dd/MM/yyyy hh:mm tt"

Code:

        [Test]
        public void NustacheTest()
        {
            Nustache.Core.Helpers.Register("FormatDateTime", FormatDateTime);
            
            var template = new Template();
            using (var sr = new StreamReader("template.mustache"))
            {
                template.Load(sr);
            }
                        
            var compiled = template.Compile<TopupReceiptModel>(null);
            var result = compiled(new { Timestamp = DateTime.Now });
            Console.WriteLine(result);
        }

        public static void FormatDateTime(RenderContext context, IList<object> arguments, IDictionary<string, object> options, RenderBlock fn, RenderBlock inverse)
        {
            if (arguments != null && arguments.Count > 0 && arguments[0] != null && arguments[0] is DateTime)
            {
                DateTime datetime = (DateTime)arguments[0];
                if (options != null && options.ContainsKey("format"))
                    context.Write(datetime.ToString(options["format"] as string));
                else
                    context.Write(datetime.ToString());
            }
        }

Template:

Transaction Details
Date & Time {{ FormatDateTime Timestamp format="dd/MM/yyyy hh:mm tt"}} 

If a remove FormatDateTime helper from the template and just use Timestamp it works.
Can somebody help please?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions