Looping through a folder of blocks

I am currently setting up a page with many products, many of them on just one page.
This results in a rather long block of content.

It would be cool to create a folder with blocks, which will be displayed on the page in a row (A littlebit like the gal tag).
e.g. {{blockloop:foldername}}}

That would be much easier to handle a lot of content.

A dedicated loop tag would be good.

I’ve done this before with things like testimonials where I want to show all of the blocks on a single page. In fact I may have shared this code before!

/* Get all of the testimonials in the testimonials directory */
function getAllTestimonials(){
    $dir = 'content/blocks/testimonials/';
    $files = glob($dir . '/*.txt');
    for ($i=0; $i < count($files); $i++){
    	echo('<div class="quote">');

You can then call the function where you want the testimonials to appear in the page;

<article class="twothird">
   <?php getAllTestimonials(); ?>

Obviously this just dumps all of the blocks in the page in the order it finds them so you may want to consider sorting them, filtering some of them (maybe blocks that are still being worked on) and paging between groups of them so you don’t end up with 1000 blocks on a very long page. :slight_smile:


Unfortunately, I can’t remember this code, but maybe because I never needed it. :slight_smile:

Thanks, I’ll try that one.
With me everything has to work with the localizer too, so let’s see how I do it in the end.

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.

Good new @Oliver - this has been added in 5.1BETA4 releasing today :slight_smile:

A new dedicated loop tag. The usage is

{{block_loop:"block folder name":"width of block including units or %"}}


This will show the blocks in the content/blocks/contact and each block will be 30% of the container width.