Replies: 5 comments 6 replies
-
I don't think it is possible the way JStachio is currently implemented. JStachio doesn't actually have stack nor can lazily load templates. The terminal state would have to be static and I can't see how that would be possible without logic that happens at compile time. The best we could do is tail recursion which can be converted to a loop but at the moment I can't remember if most template recursion is tail or augmentative. An obvious solution one would think is to use actual methods and mimic the recursion in Java but that would require moving the whole frame (the local variables on the top of the current call stack) from one method to another..... let me think about it some more maybe it is possible. So to summarize. Two problems:
|
Beta Was this translation helpful? Give feedback.
-
In the mean time do you have a specific use case other than fulfilling the spec (this is more of a curiosity)? With the exception of XSLT I always avoided template recursion in the languages that supported it. |
Beta Was this translation helpful? Give feedback.
-
Yeah we could probably do it. It would not look pretty or be fast. I'm going to ping @sviperll but we do something like: void render(...) {
// recursive template call
Object[] stack = new Object[]{ x, y, ...};
someTemplate(stack);
}
void someTemplate(Object[] stack) {
// restore variables like
String y = (String) stack[1];
Integer x = (Integer) stack[0]; // (we know at compile time what each type is)
// resume doing normal jstachio code generation.
} As a side note I was considering doing something similar to support #140 without combinatoric branch explosion (e.g. use methods instead of giant switch/if statements tree... I will explain this better later). |
Beta Was this translation helpful? Give feedback.
-
I think it's wise to support this to complete full mustache compliens (Note however that for the static-mustache project I had no such problem, because I wasn't aiming to support every mustache feature). From the project management angle this feature sounds tricky and unknown unknowy, so I wouldn't aim to support it right away. I would probably start with just expanding everything down to some fixed depth and generating code that throws a run-time exception if the actual data goes deeper. Then I would probably try to introduce stand-alone methods for partials, but having different methods for different invocations of the same partial and only then try to actually make it work so that recursive calls actually call the same method. From technical perspective I think the story can go something like this. We have a scope that at compile-time is a sequence of types, like [X; Y; ...; Z]. At run time we have variable declarations that correspond to each element from the scope. Having this we may try to generate methods for partials where each scope element is some parameter of the method: void render() {
X v1 = v0.property1(); // Entering {{#property1}} section
...
partial1(v1, v0); // render {{>partial1}}
} The last trick to notice that will make recursive calls work is to notice that if we have [X; X; Y; ...] as a compile-time scope (on the top are two elements of type X), then we can drop the second X, because there is actually no way to address it. Every mustache-placeholder will match some property from the first X or if there is no such property in X, then there is no such property in both first and second X. The result should be something like this: void recursivePartial1(X v1, Y v0) {
...
X v2 = v1.someProperty();
...
recursivePartial1(v2, v0); // we can drop v1 here
} I'm writing this on my phone, so sorry for typos and errors. |
Beta Was this translation helpful? Give feedback.
-
This (from Javadocs of
But I don't understand it. Can we have an example (whether or not it is relevant)? |
Beta Was this translation helpful? Give feedback.
-
I can see that the recursive examples in the spec are commented out of the tests, so we don't yet explicitly support that. Is there a workaround? Would it be easy to implement?
Beta Was this translation helpful? Give feedback.
All reactions