I'm trying to implement recursive algorithm solving Tower of Hanoi problem in Sightly. I know this approach may not have many obvious practical applications, I treat it as a puzzle. I end up with something like this:
<sly data-sly-template.step="${@ n, src, aux, dst}" data-sly-unwrap>
<sly data-sly-test="${n > 0}" data-sly-unwrap>
<sly data-sly-call="${step @ n = (n-1), src = src, aux = dst, dst = aux}" data-sly-unwrap/>
${src} -> ${dst}<br/>
<sly data-sly-call="${step @ n = (n-1), src = aux, aux = src, dst = dst}" data-sly-unwrap/>
</sly>
</sly>
<sly data-sly-call="${step @ n = 3, src = 'A', aux = 'B', dst = 'C'}" data-sly-unwrap/>
However, it doesn't compile as the Sightly doesn't support arithmetic
operators like -
. I don't need to count from 3 to 0, we may do it the
opposite way, as the direction doesn't matter here. I just need some
kind of counter with following features:
- we can increment or decrement it,
- we can check if equals to zero or some constant number.
I thought about using string. Empty string would be zero, 'x' would be
1, 'xx' would be 2 and so on. We can check if a string equals to a
number (n == 'xxxx'
). We can even increment it, using Sightly string
formatter:
${'x{0}' @ format = [n]}
However, the above expression can't be used as a parameter in the
data-sly-call
or in the data-sly-test
. We can only display it
immediately and no further processing is available.
Do you have any other idea if there is some counter I can use?