Including random blocks from another page.

Permalink
Have a long list of items on a page that you need to include on another page?
Then, you may want to check out using a stack.

Have a long list of items on a page that you need random items from?
Then look no further.

//First, get a reference to the page.
$storiesPage        = Page::getByPath('/PAGE_WITH_BLOCKS');
$blocks             = $storiesPage->getBlocks("AREA NAME");
//We only want blocks that are of a certain type
$eligibleBlocks     = array();
foreach($blocks as &$b) {
    $type = $b->btHandle;
    if ($type == 'YOUR_BLOCK_TYPE_ID') {
        $eligibleBlocks[] = $b;
    }
}
//Pick two blocks at random to show.
$randomBlockIndices = array_rand($eligibleBlocks, 2);
foreach($randomBlockIndices as $key) {
    $bl = $eligibleBlocks[$key];


Thanks to EC_Joe on freenode for his help.

meandmybadself
 
MrKDilkington replied on at Permalink Reply
MrKDilkington
Hi meandmybadself,

This looks interesting.

Are you open to writing it up as a How-To?
meandmybadself replied on at Permalink Reply
meandmybadself
Sure. Let me launch the site I'm using it on as a reference, and then I'd be happy to.

Can you point me in the direction of any guiding principles / instructions behind creating one?
MrKDilkington replied on at Permalink Reply
MrKDilkington
I am not aware of any guidelines for creating a How-To.

In general, thorough explanations with examples (and images depending on the topic) make for a good How-To. How-To's that consist mainly of pasted code without explanation aren't nearly as helpful.

If you want an example of top notch How-To writing, then check out anything written by JohntheFish. His How-To's are exceptionally thorough and exhaustive.
http://www.concrete5.org/documentation/how-tos?uName=JohntheFish&am...

The How-To's are written in markdown. Most people are not fluent in markdown, so creating the How-To in a markdown editor makes things much easier. I have used this online markdown editor with success -http://stackedit.io/editor .