Skip to content

Null handling when iterating String arrays/lists #202

@softwaremaverick

Description

@softwaremaverick

Given this example:

import java.io.StringReader;
import java.io.StringWriter;

import org.testng.annotations.Test;

import com.github.mustachejava.DefaultMustacheFactory;
import com.github.mustachejava.Mustache;
import com.github.mustachejava.MustacheFactory;


public class SimpleMustacheTest
{
private static class MyObject
{
	public String[] getNames()
	{
		return new String[] { "Fred", null };
	}
}


@Test
public void simpleTest()
{
	final String template = "{{#names}}{{.}}\n\n{{/names}}";

	final MustacheFactory mustacheFactory = new DefaultMustacheFactory();
	final Mustache mustache = mustacheFactory.compile(new StringReader(template), "test");

	final StringWriter writer = new StringWriter();
	mustache.execute(writer, new MyObject());

	System.out.println(writer.toString());
}
}

Why do I get this?

Fred
SimpleMustacheTest$MyObject@4891a775

Effectively it looks like it's saying that if a value cannot be extracted for an element then I'll take the parent scope. If I'm doing a {{.}} then that parent scope seeking shouldn't happen should it? It's not like anyone would be saying that if I don't have a value then get my parent instead would they?

If this is a design decision then does that mean we have to wrap all objects in true/false checks?

Currently the scenario in the real code I'm forcing the data to always return empty string if the value is a null to overcome this issue.

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