Hello! I have a range of the 20 element. I need every 4 element to pastle the HTML content. After 20-th elements no need to pastle the code. How do that?
In PHP I have the code
foreach ($array as $key => $value) {
if(is_int($key/4) AND $key < count($array)) {
echo "html_code";
}
}
The following snippet should be equivalent to your PHP script:
{{ range $index, $value := first 20 $list }}
{{ if modBool $index 4 }} $value {{ end }}
{{ end }}
3 Likes
Thanks. But I need more dificult condition.
foreach ($array as $key => $value) {
if(is_int(($key+1)/4) AND ($key+1) < count($array)) {
echo "html_code";
}
}
or
$j = 0;
foreach ($array as $key => $value) {
$j++;
if(is_int(($j)/4) AND $j < count($array)) {
echo "html_code";
}
}
If it is possible, then I would not like to be attached to the number of array elements (that is, there may be 30 or more values)
I adapted the script above slightly but hadn’t time to test it. But you should get the idea:
{{ range $index, $value := first 20 $list }}
{{ $inc := add $index 1 }}
{{ if modBool $inc 4 | and ( lt $inc (len $list) )}}
$value
{{ end }}
{{ end }}
Longer if statements could be broken up in smaller parts such that you only need to compare the results of the partial condition (e.g. the index is divisible by four):
{{ if $cond1 | and $cond2 }}
Regarding your second example: $index
would be equivalent to you $j
because the index is incremented automatically. If you want to test $j < count($array)
you can use the partial condition from the first example where you have to replace $inc
with $index
in ( lt $inc (len $list) )
1 Like