I need php help

Permalink 2 users found helpful
Im working on a theme and I need to wrap all the blocks in one area in a div called .site-spacer. I have this so far:

<?php       $a = new Area('Sidebar');
                if ($a->getTotalBlocksInArea() > 0) {
               echo ('<div class="site-spacer">');
             }
               $a->setBlockWrapperStart('<div class="inner">');
               $a->setBlockWrapperEnd('</div>');
               $a->display($c);
               if ($a->getTotalBlocksInArea() > 0) {
               echo ('</div>');
             }
         ?>


But it pulls the $a from the whole page, I think. Anyway, it doesn’t work and was wondering if someone could tell me what Im doing wrong.

tallacman
 
Mainio replied on at Permalink Reply
Mainio
I'm not sure if I completely got your question but checking the code, I think you should pass the current view to the area functions:
// Add $c as an argument in the function call
$a->getTotalBlocksInArea($c)


Sorry if I misunderstood you, just my best guess!

Antti / Mainio
12345j replied on at Permalink Reply
12345j
thats sorta weird, what part of it isn't working?
found this comment on the getTotalBlocksInArea function
@param Page $c must be passed if the display() method has not been run on the area object yet.
if its the </div> part thats not working, then I'd assign a separate variable when you check the first time. so like
if($a->getTotalBlocksInArea()>0){
$enddiv=1;
....
if($enddiv==1){
echo '</div>';
}
mkly replied on at Permalink Reply
mkly
@12345j speaks the truth about passing the page in.

I use this all the time because I hate including empty divs if there is no blocks in the Area.
$a = new Area('Sidebar');
if($a->getTotalBlocksInArea($c)) {
aghouseh replied on at Permalink Best Answer Reply
aghouseh
I think this is what you're looking for. Suppress the wrapper and area itself unless there is content (or you are in edit mode).

<?php
$a = new Area('Sidebar');
$a->setBlockWrapperStart('<div class="inner">');
$a->setBlockWrapperEnd('</div>');
if ($a->getTotalBlocksInArea($c) > 0 || $c->isEditMode()) {
 echo ('<div class="site-spacer">');
 $a->display($c);
 echo ('</div>');
} 
?>
mkly replied on at Permalink Reply
mkly
This whole discussion is a total pet peeve of mine. This is how I currently do Areas in general and I wish the default themes did this.

At the top of each page_type file I set up all my Areas
<?php
  $main = new Area('Main');
  // This part allows much easier reach via css
  $main->setBlockWrapper('<div class="area-main area-block">');
  $main->setBlockWrapper('</div><!-- /area-main -->');
  $show_main = ($main->getTotalBlocksInArea($c) || $c->isEditMode());
  $sidebar = new Area('Sidebar');
  $sidebar->setBlockWrapper('<div class="area-sidebar area-block">');
  $sidebar->setBlockWrapper('</div><!-- /area-sidebar -->');
  $show_sidebar = ($sidebar->getTotalBlocksInArea($c) || $c->isEditMode());
?>


Then in the body of the page_type I do
<?php if($show_main): ?>
  <div id="main">
    <?php $main->display($c); ?>
  </div>
<?php endif;?>
<?php if($show_sidebar): ?>
  <div id="sidebar">
    <?php $sidebar->display($c); ?>
  </div>
<?php endif; ?>


Then you don't show divs that don't have anything in them.

Also the view part is little nicer looking as well.
aghouseh replied on at Permalink Reply
aghouseh
This is pretty slick. I might have to lift this; nice work!
mkly replied on at Permalink Reply
mkly
I am in the slow(last priority) process of getting all of these things wrapped up in that gogo thing I was talking about a little while ago. If you want to go all out you can define your areas in page_type controllers instead of the view. I've been getting more into them lately. I might even pull moustache into the whole gogo thing, although that feels a little too trendy even for me.
tallacman replied on at Permalink Reply
tallacman
Thank you, all. That worked!