Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Partials API #20

Open
cgarciae opened this issue Apr 26, 2015 · 2 comments
Open

Partials API #20

cgarciae opened this issue Apr 26, 2015 · 2 comments

Comments

@cgarciae
Copy link

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
Copy link
Owner

xxgreg commented Apr 26, 2015

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
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants