Count columns in a layout

hello, happy Sunday :slight_smile:

I’m trying to build an HTML email newsletter compiler using Kirby 3.5 layouts and blocks.

For a single column content module, I’m wrapping the layout in a table and looping through blocks for the content is straightforward.

But for a multiple column layout, I want to add additional nested tables around each column.

My idea to do this is to use an if/else statement based on the number of columns in the layout. 1 column, just loop through the blocks. More than 1, then add the additional nested table.

But I can’t find a method for counting the columns in the layout.

My guess is:

<?php if ($layout->columns() > 1): ?>

This is how I think it should work…

			<!-- start container -->
		<table class="container fluid-table" border="0" cellpadding="0" cellspacing="0" width="720" role="presentation">
		<tr>		<td class="wrapper">
					<?php foreach ($page->nlContent()->toLayouts() as $content): ?>
					<table border="0" cellpadding="0" cellspacing="0" width="100%" role="presentation">
					<tr>		<td align="left" style="padding: 30px 20px 20px 20px;">
								<?php if ($layout->columns() > 1): ?>
								<?php foreach ($content->columns() as $column): ?>
								<table border="0" cellpadding="0" cellspacing="0" width="47%" align="left" role="presentation">
								<tr>		<td class="left">
											<?= $column->blocks() ?>
								</td>	</tr>
								</table>
								<?php endforeach ?>
								<?php else: ?>
								<?php foreach ($content->columns() as $column): ?>
								<?= $column->blocks() ?>
								<?php endforeach ?>
								<?php endif ?>
					</td>	</tr>
					</table>
					<?php endforeach ?>
		</td>	</tr>
		</table>
		<!-- end container --> 

But this throws up a Call to a member function columns() on null on that if statement.

Plan B for this is to build a custom block for multiple column modules, but that feels less elegant…

$layout in your snippet is a variable that doesn’t seem to be defined anywhere.

A single layout in your loop is in the $content variable, and so you should be able to do

$noOfColumns = $content->columns()->count();

Thank you, it worked!

I was working from this this page in the reference ($layout->columns() | Kirby)

I had taken $layout to be something like $page or $site…