Simple MVC system in Drupal 6

I've recently started working on a custom module in Drupal 6 and i really disliked the idea to use built in functions for tables and/or to use embedded html into my module.
So I've made a simple system on having a view like system in drupal

Create a new module, and in the module folder create a directory called views (for eg).
Add the following function in your module

  1. function get_include_contents($file, $file_data) {
  2.     extract($file_data);
  3.  
  4.     if (is_file($file)) {
  5.         ob_start();
  6.         include $file;
  7.         return ob_get_clean();
  8.     }
  9.     return false;
  10. }

You can then do it like so

  1. function mymodule_page()
  2.     $view = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR . 'mymodule_page.php';
  3.     $view_data = array(
  4.         'variable_name' => 'data',
  5.     );
  6.     $page_content = get_include_contents($view, $view_data);    
  7. }

And you'll be able to do a <?php echo $variable_name; ?>

Technology: 

Add new comment