xxgreg / mustache

Mustache template Dart library
BSD 2-Clause "Simplified" License
57 stars 59 forks source link

Partials API #20

Open cgarciae opened 9 years ago

cgarciae commented 9 years ago

Given that templates already have names, wouldn't it be easier to resolve partial by template names? You'd have to create the field List<Template> partials. But this example

var partial = new Template('{{ foo }}', name: 'partial');

var resolver = (String name) {
   if (name == 'partial-name') { // Name of partial tag.
     return partial;
   }
};

var t = new Template('{{> partial-name }}', partialResolver: resolver);

var output = t.renderString({'foo': 'bar'});

could be reduced to something like this

var partial = new Template('{{ foo }}', name: 'partial-name');

var t = new Template('{{> partial-name }}', partials: [partial]);

var output = t.renderString({'foo': 'bar'});
xxgreg commented 9 years ago

I want to make sure that this library can be used with lazily loaded templates. I.e. you may not have already loaded and parsed the list of partials at the time you create a template.

But it should be pretty easy to come up with a utility function to do what you want. Perhaps it would make sense to include something like this in the library.

Here's a quick example:

PartialResolver templateResolver(List<Template> templates) {
  var map = new Map.fromIterables(templates.map((t) => t.name), templates);
  return (String name) => map[name];
}
cgarciae commented 9 years ago

Can you create a constructor for Template that also accepts a List<Template> partials and does what you proposed? I am creating a plugin for Redstone based on mustache, not having to implement that funtion would better long term.