2

I am trying to test out the functionality of SmartFormat.NET, and I am having trouble trying to format a list of view model items. According to this exchange, what I am trying to accomplish should be possible with nested placeholders.

Here is the template I am using:

var smartTemplate = @"
<div>This is the title</div>
<div>model name: {Name}</div>
<div>model description: {Description}</div>
<div>model rating: {Rating}</div>
{ItemList:
    <div>{NestedName} has number equal to {Number}</div>
}";

And my view model(s):

public class SimpleTestVM
{
    public string Name { get; set; }
    public string Description { get; set; }
    public int Rating { get; set; }
    public NestedSimpleVM[] ItemList { get; set; } 
}
public class NestedSimpleVM
{
    public string NestedName { get; set; }
    public int Number { get; set; }
}

Then to format the data I initialize a view model with a list of a few items and then use the following code:

Smart.Default.AddExtensions(new ListFormatter(Smart.Default));
Smart.Default.AddExtensions(new ReflectionSource(Smart.Default));
var smartResult = Smart.Format(smartTemplate, model);

On the call to Format, I get the following error:

Error parsing format string: Error parsing format string: Could not evaluate the selector "NestedName" at 165
{... includes the template  here...}

Digging into the source code it seems that SmartFormat thinks the NestedName selector was not handled, and that is why it is throwing an error. I can't figure out why it does this; as far as I can tell it follows the syntax correctly.

Ian Ringrose
  • 51,220
  • 55
  • 213
  • 317
mao47
  • 967
  • 10
  • 25

1 Answers1

5

With some more poking around through the source, I found the issue. The ListFormatter requires a "|" character to denote the separator for the formatted items, so I changed my format to:

var smartTemplate = @"
<div>This is the title</div>
<div>model name: {Name}</div>
<div>model description: {Description}</div>
<div>model rating: {Rating}</div>
{ItemList:
    <div>{NestedName} has number equal to {Number}</div> | }
";

This works fine. Now to figure out how to conditionally display items based on their properties.

mao47
  • 967
  • 10
  • 25