rocker
rocker copied to clipboard
@with limitations, lack of real scoped code blocks and functions
I was wondering if there's a more intuitive or simple approach to declare variables within a templates. I find the current @with implementation quite limiting, since I want to handle all the templete logic on the templete itself (Without calling custom methods on a separated class)
Currently I'm forced to declare all the variables that my template depends inside a @with
Template:
@with(
String a = "hello",
String b = "world"
) {
This is an example:
a: @a
b: @b
}
Output:
This is an example:
a: hello
b: world
This means that if want to use "a" and "b" on all my template I have to put all my template inside the with curly bracers (Note: I need all logic to be on the template so args wont do the trick)
I think this is inconvenient and restrictive, a different approach would be to allow some sort of scoped code shim that allow arbitrary java code to be written on the template with the scope of that template, in a similar way that other languages like php does it.
for example ( As an example draft)
Template:
@block {
String a = "hello";
String b = "world";
}
This is an example:
a: @a
b: @b
Output:
This is an example:
a: hello
b: world
Here @a and @b are within the template scope itself.
Also functions/methods on templates will come in handy for example someone could write a template holding functions and import that template on a different one, currently this behaviour can only be mimic by having each "function" or reusable piece of code as an independent template and importing it with args as parameters when desired
one again as a draft example: (note @1 and @2 acts as param1 and param2 and content blocks are used as pseudo functions, but ideally they should have their own scope and access to the template scope to avoid the need on @with within them)
Template one:
@add => {
@with(int result = @1 + @2) {
@result
}
}
@subtract => {
@with(int result = @1 - @2) {
@result
}
}
Template two:
@import resources.one.rocker as templateOne
@block {
int a = 2;
int b = 3;
}
@a plus @b is @templateOne.add(@a, @b)
@a minus @b is @templateOne.subtract(@a, @b)
Output:
2 plus 3 is 5
2 minus 3 is -1
Any thoughts about this??? I'll personally would like to develop this in collaboration of your team as long as you think I'ts a good idea, I'm really loving rocker and I believe this is a huge enhancement to its posibilites.
add / subtract should be done in Java. Use a custom application-specific context.
For ninja/rocker
https://github.com/fizzed/ninja-rocker
This feels like it is forcing the creation of a DSL that replicates Java functionality, when with Rocker you should be able to drop into Java.