Answer the question
In order to leave comments, you need to log in
How to organize the output randomly?
Hello, I have two chunks with blocks:
1) $lib.block.profile.3
2) $lib.block.profile.7
I need to display them in the template, only that each time they visit the page, they change places.
I have a snippet that I display in a template, now it looks like this:
<?php
$output = 'lib.block.profile.7';
if ($modx->resource->id==54) {
$output = 'lib.block.profile.3';
}
return $modx->getChunk($output);
Answer the question
In order to leave comments, you need to log in
$output = random_int(0, 1) ? 'lib.block.profile.7' : 'lib.block.profile.3';
// Список чанков оформляется в массив
$chunks = [
'lib.block.profile.7',
'lib.block.profile.3',
];
if ($modx->resource->id==54) {
$output = $chunks[1]; // Не знаю, нужно ли вам это условие
} else {
// Но если оно не выполняется, в $output кладется случайное значение чанка из массива
$output = $chunks[rand(0, count($chunks) - 1)];
}
// Возвращается обработанный чанк
return $modx->getChunk($output);
You need to display them in the template, only that each time you visit the page, they change places
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question