kainjow/Mustache

wrong render result for section

yangqinj opened this issue · 2 comments

Here I have a template with a section

static const char* temp =  R"(
{{#path_handlers}}
 <li><a class="nav-link"href="{{path}}">{{alias}}</a></li>
{{/path_handlers}}
)";

To render this template, I use the following code to fill the tag:

  data data_path_alias{data::type::list};
  data_path_alias << data{"path", "/"} << data{"alias", "Home"};
  data_path_alias << data{"path", "/config"} << data{"alias", "Configuration"};
  data data_path_handlers{"path_handlers", data_path_alias};

  using mycontext = context<mustache::string_type>;
  mycontext ctx;
  ctx.push(&data_path_handlers);

  cout << tmpl.render(ctx) << endl;

The resulting output is:

<li><a class="nav-link"href="/"></a></li>

 <li><a class="nav-link"href="">Home</a></li>

 <li><a class="nav-link"href="/config"></a></li>

 <li><a class="nav-link"href="">Configuration</a></li>

There two wrong points:

  • the path and alias are not on the same line
  • there are redundant empty lines between every

Did I wrongly use the data with data::type::list?

Sorry for the delay. My email was broken :)

What version are you using?

So looking at this again, yes, you're adding 4 items to the list, so it's:

1: data{"path", "/"}
2: data{"alias", "Home"}
3: data{"path", "/config"}
4: data{"alias", "Configuration"}

You need to use objects (unordered maps). There are some examples in the tests: https://github.com/kainjow/Mustache/blob/master/tests.cpp#L993

Also you shouldn't need to use the context class directly, just pass data_path_handlers to render instead.