[TYPO3-dev] Another TypoScript looping solution
Davide Principi
d.principi at provincia.ps.it
Thu Jun 9 10:03:01 CEST 2011
Hi all,
I've read about the `.split`-based looping techniques in TypoScript and
I'd like to share another TypoScript looping solution exploiting CASE,
registers, current-value and recursive calls that I'm applying to my
TS-templates.
I state that what I'd been searching for was an extensible fallback
mechanism to generate a thumbnail image, trying a list of methods until
one generates the image successfully (attached image, default image,
text image...).
The solution has
a) the list of image generating methods to attempt
b) the recursive call to the next method
c) the exit condition
An outline of the TypoScript code follows (don't cut&paste it):
lib.recursiveLoop = CASE
lib.recursiveLoop {
key {
#
# (a) use a register to hold the list or methodX
# if empty
#
data = register : iterations
ifEmpty = methodX
split {
token = ,
returnKey.current = 1
}
}
method1 =< ...
method2 =< ...
.
.
.
methodN =< ...
#
# As a last resort we MUST return a non-empty value to avoid
# an infinite loop!
#
default = TEXT
default.value = ...
#
# (c) the next condition (otherwise terminates)
#
stdWrap.ifEmpty {
#
# (b.1) Increment the list pointer, stored in the
# current-value:
#
setCurrent {
current = 1
stdWrap.wrap = |+1
prioriCalc = 1
}
#
# (b.1) Recursive call
#
cObject =< lib.recursiveLoop
}
}
To start a loop that selects a subset of available methods we have to
load the "iterations" register. We must also ensure that the
current-value is 0:
lib.callLoop1 = COA
lib.callLoop1 {
10 = LOAD_REGISTER
10.iterations = method3,method5
20 = TEXT
20 {
setCurrent = 0
cObject =< lib.recursiveLoop
}
30 = RESTORE_REGISTER
}
That's all: I hope you like it.
--
Davide Principi - http://www.docweb.provincia.pu.it/
Ufficio 2.3.1.2 - Gestione Rete Telematica Provinciale
Amministrazione Provinciale di Pesaro e Urbino
Via Mazzolari 4 - 61121 Pesaro PU
More information about the TYPO3-dev
mailing list