Author Topic: Give each block separate classes  (Read 4875 times)

pushloop

  • Guest
Give each block separate classes
« on: September 05, 2007, 07:52:19 PM »
Hi y'all

I'm trying to set up page_content(2) and page_content(3) to show up with a frame around each using CSS.
I could place each content inside a div, and then give both divs the same class. But the problem then is that I would have to fill both page_contents with text, otherwise the borders will show up anyway, leaving just an empty box.
Basically, I would like the boxes with borders to show up only if there is any content!

Is there any way to automatically give each content a "<div class="something"> text </div>" by modifying the php-code?

Any help or ideas asap would be highly appreciated!
Tnx
/André

Offline marathoner

  • Posts: 495
Re: Give each block separate classes
« Reply #1 on: September 05, 2007, 09:14:57 PM »
Just modify your template to check to see if a certain section exists before you include that section. I have modified my template to display various columns (actually blocks) if a page has two blocks otherwise use the entire width to display the contents if there is only one block. Here's a snippet from my template:

Code: [Select]
<?php ob_start(); // Start the outputbuffer
page_content(2); // Next call the block
$content2=ob_get_contents();  // Now fetch the output into a variable
ob_end_clean(); // Clean up old mess and stop buffering
?>

<div id="content">
<?php if ($content2<>"") { // Next test $content2 to see if there is something in it
   
echo "<div id=\"content1\">\n";
}
?>

<?php page_content(1);
    echo 
"\n</div><!-- close div#content1 -->\n";
?>

<?php if ($content2<>"") { // Next test $content2 to see if there is something in it
    
echo "<div id=\"content2\">\n";
    echo 
$content2;
    echo 
"\n</div><!-- close div#content2 -->\n";
}
?>

<?php if ($content2<>"") { // Close the div tag for $content2 if it was used
   
echo "</div><!-- close div#content -->\n";
}
?>


Offline DGEC

  • Posts: 388
  • Gender: Male
    • EbelTech homepage
Re: Give each block separate classes
« Reply #2 on: September 08, 2007, 03:16:28 AM »
Beautiful solution, Marathoner.
I'm raw with PHP, didn't know you could do something like that. Cool!

Lotus

  • Guest
Re: Give each block separate classes
« Reply #3 on: March 25, 2008, 08:18:41 AM »
Great..saved my project  :-D. Thank you very much!

Offline spida

  • Posts: 203
Re: Give each block separate classes
« Reply #4 on: April 07, 2008, 01:55:39 PM »
Hey,

great solution!
I might misunderstand your code, marathoner, but I think there is a tiny error in it.
In
Code: [Select]
<div id="content">
<?php if ($content2<>"") { // Next test $content2 to see if there is something in it
   
echo "<div id=\"content1\">\n";
}
?>
shouldn't it be instead
Code: [Select]
<div id="content">
<?php if ($content1<>"") { // Next test $content1 to see if there is something in it
   
echo "<div id=\"content1\">\n";
}
?>
?

Offline marathoner

  • Posts: 495
Re: Give each block separate classes
« Reply #5 on: April 07, 2008, 03:43:40 PM »
No, that is not an error. Here's my logic...I already know that every page will have something in content1 but only some pages will have both content1 and content2.

I only want to use a right and left column (div1 and div2) if there are two content blocks...so I need to test to see if there is anything in content2. If there is no content2 then content1 doesn't need wrapped by div1 (which I use for a right column. This means that content1 only pages will occupy the full width and that pages with content2 will use a right and left column. Make sense?

Offline spida

  • Posts: 203
Re: Give each block separate classes
« Reply #6 on: April 07, 2008, 09:33:49 PM »
Hi Marathoner,

your explanation makes perfectly sense. Thank you!

Regards,
spida