On Sat, Mar 26, 2016, at 05:32 PM, Sam Pullara wrote:
> Ok, now that I understand what you want I can definitively say I won't be implementing it in the core. It is too much like a mini programming language like filters / data formatters and the like. I have a proposal for you though.
>
> You can't actually implement it using a MustacheResolver as partials are statically evaluated at compile time — so 1&2 won't work.
I don't understand how you could stop me :)
I just have to implement something like this:
MyResolver implements MustacheResolver {
Map<String,String> bindings; //injected by me
// replace any [foo] with bound value first
public Reader getReader(String resourceName) {
Matcher m = Pattern.compile("\\[.*?)\\]").matcher(resourceName);
StringBuffer sb = new StringBuffer();
while (m.find()) {
String sym = m.group(1);
String v = bindings.get(sym);
if (v == null || v.isEmpty()) throw new IllegalArgumentException("no value for '" + sym + "'");
m.appendReplacement(sb, v);
}
m.appendTail(sb);
resourceName = sb.toString();
// now resolve normally
...
}
> A Function won't work either as you won't have the scope context to pass along.
Well, I gave you the scopes so i know what they are :).
Why couldn't I do a function to support:
{{#asPartial}}all/my/blurbs/{{id}}.txt{{/asPartial}}
like
static Function<String,String> asPartial = new Function<String,String>() {
public String apply(String v) {
Reader r = my_resolve(v);
return reader_to_string(r);
}
};
>I've left open all the hooks though so you could implement it by overriding the creation of PartialCode() in the MustacheVisitor. I've checked in a test case that implements a version of this:
>
>
https://github.com/spullara/mustache.java/commit/eaee8ce2640413b20ddb405b8ee8018d73da913c#diff-f5e8866b19228187afb247b0ed8629f4R647
>
> The syntax is {{>+a/[foo]/[bar].txt}} — it will do text replacement using mustache in the name with the alternate delimiters. It then compiles the newly resolved partial and caches it. This leaks memory if you created an unlimited number of dynamic partials and it also doesn't have any depth protection for recursion, but it seems to do what you were looking to do.
There must be something fundamental about the mustache.java compile-time versus run-time that I'm not understanding,
in addition to my not knowing about any caching.
Are you saying that builtin to mustache.java is a cache layer and it will never call my resolver more than once
for the same string?
-mda