Outputting dynamic areas within a loop

Permalink
Hello

I am trying to create dynamic page layouts. I have added a textarea as a page attribute in which I enter a layout in .ini format.

Example
[Header]
Row1=4,8
[Main]
Row1=12
Row2=4,8
Row3=12


I am then parsing the text and looping through it to output areas.

It all works fine apart from the actual area output

foreach($span as $s){
...
$ah = new Area('HeaderRow'.$row.'Col'.$col);
$ah->display($c);
...
}


I am guessing that I cannot use display inside a loop but am unfamiliar with the details of object programming. A normal print or echo works fine in the loop.

Any input would be greatly appreciated.

Thanks

digirunt
 
JohntheFish replied on at Permalink Reply
JohntheFish
What you are doing should work in principle.

However, you need to make sure you have the most used areas in the page mapped to the usual c5 area names (which are case sensitive). Otherwise you will have content disappearing (and reappearing) whenever you switch themes (going to a default theme is usually a good idea during upgrading or when debugging)

You will also need to think about what you are going to do with page type defaults.

This is something I have been meaning to have a play with at some time, but never got round to, so please keep us posted on your progress.
digirunt replied on at Permalink Reply
digirunt
Hi

Outside of theme switching I have always wondered what other reason is there to stick to the C5 area naming convention.

I was hoping to be able to set the number of rows and columns per page and have an area created for each. If no layout is specified I am falling back to the usual 'Main' areas etc.

The only problem I am having is the

$ah->display($c);


does not create areas when used inside a loop within the page type.

I am still creating page types [default, view, full etc..]. But rather than have lots of page types for every possible combination of rows and columns and sidebars etc I want to specify the full page type for each page then set the layout within the page.
JohntheFish replied on at Permalink Best Answer Reply
JohntheFish
Maybe experiment with unwrapping the loop into equivalent serial code and see what happens. That will either confirm or eliminate the loop as being responsible.

You could also throw a bit of debug code in to dump the objects as the loop runs.
http://www.concrete5.org/documentation/how-tos/developers/concrete5...

After that, maybe make $ah into an indexed array of $ah, so area objects don't get recreated over each other.
digirunt replied on at Permalink Reply
digirunt
Thanks to your suggestion I dug in deeper and found a really obvious mistake. I was using..

foreach ($layout['Header'] as $r => $c) {


which was re-declaring $c meaning that the area couldn't output using the built in $c page object.

It all works great now.


The idea was that I can have a text area attribute (could be something nice and graphical in future) which allows me to set the pages layout. This means the layout is determined on a per page basis and can have any number of rows and columns each with it's own area.
I kept the 'Header' and 'Main' areas above my custom layout just for legacy reasons (if any).

So an attribute like

[Header]
Row1=4,8
[Main]
Row1=12
Row2=4,8
Row3=8,4
Row4=12


Outputs a page like

<div class="header container">
  <div class="row">
    <div class="span4">
      <!-- Area called HeaderRow1Col1 -->
    </div>
    <div class="span8">
      <!-- Area called HeaderRow1Col -->
    </div>
  </div>
</div>
<div class="main-container">
  <div class="row">
    <div class="span12">
      <!-- Area called MainRow1Col1 -->
...


I'm using the Bootstrap framework but it would work with any css.
I've also changed the default C5 red dotted borders to outlines to ensure the bootstrap areas don't wrap due to the extra width C5 adds in edit mode.

It works in a similar way to C5's native layout feature but without inline styling and with Theme applied layout structure.

I think column and row layout is something other users have asked about previously so I'd be interested to know how other people are doing this or your suggestions.

Thanks
JohntheFish replied on at Permalink Reply
JohntheFish
There is already the c5 layouts tool for working with layouts within areas, but it is a bit of a pig.

I would be tempted to make the ini format the other way round. Perhaps a bit more complex to parse, but maybe you could then say which row & cols spread was Main, Sidebar etc, so getting better use of the standard names.

This would also stop areas from disappearing when you change the attribute a little and every name consequently changes.