Split theme('page') $content into multiple sections

This snippet is based on PHPTemplate. Other engines will work, but you'll have to put the helper function somewhere else.

If you need to break up the a list of nodes on a page, this simple solution should work for you.

  1. At the end of your node.tpl.php file, add an HTML comment to serve as a delimiter. I use <!-- NODESPLITDELIMITER -->.
  2. In your template.php file add the following function to split up content based on a delimiter:
    <?php
    function _contentsplitter($incontent, $delimiter, $headlength)
    {
     
    $outcontent = array();
     
     
    $tmp = explode($delimiter, $incontent, $headlength + 1);
     
      for (
    $i = 0; $i < $headlength; $i++) {
       
    $outcontent[0] .= $tmp[$i];
      }
     
     
    $outcontent[1] = $tmp[$headlength];
     
      return
    $outcontent;
    }
    ?>

    If you need more than 2 sections, you can modify this accordingly.
  3. If you have not already created a function _phptemplate_variables($hook, $vars) in your template.php file, create one like this:
    <?php
    function _phptemplate_variables($hook, $vars) {
       switch(
    $hook) {
         case
    'page' :
           
    $content_sections = _contentsplitter($vars['content'], '<!-- NODESPLITDELIMITER -->', 2);
           
    $vars['content_head'] = $content_sections[0];
           
    $vars['content_tail'] = $content_sections[1];
           
            break;
       }
       return
    $vars;
    }
    ?>

    Again, if you need more than 2 sections, modify accordingly. You could also just pass the sections array to your page.tpl.php file and then iterate over it.
  4. In page.tpl.php you now have access to two segments of the content. You could use it like this:
    <?php


    print $content_head;
    print
    $sidebar_left;
    print
    $content_tail;
    print
    $sidebar_right;
    ?>

    And style the side bars according.

Good luck!

To accomplish this with the Smarty theme-engine

tclineks - January 19, 2006 - 04:06

To accomplish this with the Smarty theme-engine the changes would be the creation of ( or addition to) the smartytemplate.php file instead of the template.php file and naming the variables function _smarty_variables instead of _phptemplate_variables.

How about splitting terms into multicolumn

kc - March 24, 2006 - 20:48

How can the term listing be splitted into multi-columns? I am using taxonomy context and I already searched the site several times with no luck.

Here is the site where I need to split the output of terms into at least 3 or 4 columns so that it is easier to navigate.

http://www.realestatecentury.com

thanks

kc

accompished

kc - March 27, 2006 - 21:35

I accomplished what I wanted and here is the change I made to my taxonomy_context module if anyone is interested in it.
Note: I am not an advanced programmer, so the code may not be tidy. You are welcome to modify and/or expand it.

function taxonomy_context_show_subterms($tid, $vid = 0) {
  $terms = taxonomy_context_get_subterms($tid, $vid);
  $output = '';
  if (!(empty($terms))) {
  $count=0; //kc
  $col=4; //kc
  $output = '<table><tr>'; //kc
    $output .= "<div class=\"clearit\"></div>\n";
    foreach ($terms as $term) {
      $links = array();
      if (user_access('administer taxonomy')) {
         $links[] = l(t('administer'), "admin/taxonomy/edit/term/$term->tid", array('title' => t('Administer this term.')));
      }
      if (module_exist('node_image') && user_access('create node and term images')) {
        if (!node_image_file_exists($term->tid, 'term', 'full')) {
          $links[] = l(t('add images'), "node_image/add/term/$term->tid", array('title' => t('Add images for this term.')));
        }
        else {
          $links[] = l(t('replace images'), "node_image/add/term/$term->tid", array('title' => t('Replace images for this term.')));
        }
      }
      $term->image = taxonomy_context_get_img($term, 'term', 'summ');
      $term->links = $links;
      $term->title = $term->name;
  if ($count < $col){ //kc
    $output .= '<td align=top>' . theme('taxonomy_context_term', $term, 'subterm'). '</td>';
  }
  else {
    $output .= '</tr><tr><td align=top>' . theme('taxonomy_context_term', $term, 'subterm'). '</td>';
  $count = 0;
  }
  $count++;
    }
  }
  $output .= '</tr></table>'; //kc
  return $output;
}

Only working with HTML filter turned off

enli - January 22, 2008 - 10:29

This snippet works in drupal 5.x only with HTML filter turned off.
I found out that in this snippet there is a problem of "input formats". In fact, when I chose "Filtered HTML" (HTML filter active), the splitted content is displayed in the first column following the first part of content, whereas if I chose "Full HTML" (HTML filter inactive), the splitted content is correctly displayed in the second column.

There seemed to be a problem of reading with the filters, so I tried to add to the list of allowed HTML tags, but this doesn't change the result: "Filtered HTML" isn't working with , so that the HTML comment is filtered and the splitted content is displayed in the first column, as if wasn't put at all in the node text.

It is clear that the problem is with the "HTML filter", since when I turn on HTML filter in the "Full HTML" settings page, isn't read anymore. By turning off the HTML filter,
is read and the content is correcty split. I also tried to add in the text and/or in the HTML filter <!-- NODESPLITDELIMITER -->, but nothing changed.
Do you think there is a solution to this problem?

enrico

enrico

http://www.enricolippi.net

Tried converting this to Drupal 6, but can't get it working.

durruti - December 8, 2008 - 03:07

Hi Folks,
I've been trying to get this working with Drupal 6 but it's late, my brain is tired and my php is not up to it.

<?php

function openspace_preprocess_page(&$vars, $hook) {
       
$content_sections = _contentsplitter($vars['content'], '<!-- NODESPLITDELIMITER -->', 2);
       
$vars['content_head'] = $content_sections[0];
       
$vars['content_tail'] = $content_sections[1];
    return
$vars;   
}

?>

Also when I have multiple converts to add to preprocess page, do I just stack them, i.e remove the top and bottom two lines (in the case of the above code) for each new one I add and place them above the "return $vars; }' command? Do I need some kind of seperator?
Many thanks for any tips, cheers,
Durruti

Me too

exotec - December 12, 2008 - 05:37

I'm also looking for a working solution for text in two columns in Drupal 6. Maybe you (or anybody who has - or better had - the same problem) could post something, if you found out something about this issue?
Thanks in advance

 
 

Drupal is a registered trademark of Dries Buytaert.