Drupal Views module may be the most powerful, useful and handy tool for Drupal webmasters, The Views2 module gets much better and powerful.

We received a request from one of our client that they wanted  to list all of their newest ratings tables on the front page. We had done such job by customize a view filled with CCK fields and set the view as the default front page.

This time, I want a different approach to this request,a  more extensible, more professional way.

Here is the solution I used.

  1. Create all views to be displayed on the front page, like foo-view, bar-view, etc. And theming them as usual.
  2. Create a new view which you will set it as the new front page, or just enable the front page emulation view from the Views 2 distribution. Name it frontpage-view or something meaningful.
  3. On the frontpage-view edit page,  configure all parameters for normal front page display, such as filters for display news, blog posts, etc.
  4. Here comes the magics: Lets suppose we want to display the previous created foo-view, bar-view on the to of our new  front page. Choose the “Page” display of the frontpage-view, click “Header” link in the “Basic settings” section, choose PHP code as the input format.(If it was not available, enable the module first.)
  5. Code in the “Header” textarea:
    <?php
    print views_embed_view(“foo-view”,”page”);
    print views_embed_view(“bar-view”,”page”);
    ?>
  6. Update the settings.
  7. Set the “Path”  of the “Page settings” section as “frontpage” or something meaningful,  update the settings and save the view.
  8. On the “admin->settings->site information” page, set the “Default front page” path as “frontpage” just same as #7 settings. And save.

OK, now just reload your home page, everything should be displayed as we supposed.

Note: The “views_embed_view” function is introduced to Views 2, not available in Views 1.
Tips:

  • If you want the foo-view, bar-view display on the bottom of the frontpage-view, move the #5 code to “Footer” settings;
  • If the frontpage-view got nothing to display, the whole page will be blank, to make it work in a blank “container”,  make the “Display even if view has no result” option checked, the option is just above the textarea which you enter the #5 code.
  • You can pass parameters to the “views_embed_view” function of #5 code, details goes here.

Make no sense? Ok, maybe I should post some basic tutorials above Views 2 some day to make this post easy to understand.