Community Documentation

Returning part of a page

Last updated February 11, 2011. Created by sunset_bill on February 11, 2011.
Log in to edit this page.

I had a requirement where an outside server wanted to use content from a Drupal site. The Services module does some of that, but requires an XML or JSON RPC call. So, in addition to Services and a custom module that can write out files, I added the following to at the top of my theme, before the !DOCTYPE tag

<?php
// make me smart
// just return the part of the page the caller wants
 
$context = $_GET['context'];
  switch (
$context) {
    case
"content_only":
      print
$content;
      return;
   ...
  }
?>

The caller can hit my site with
http://example.com?context=content_only

and get just the HTML for the content section of the page. If no context is specified in the URL, the user gets the whole page. Depending on the theme you're using, this can work for menus, sidebars, regions, etc.

Comments

Clarification

This goes at the top of page.tpl.php. If you're using a theme that implements preprocess_page() in template.php, you can put $context = $_GET['context'] preprocess_page().

Page status

No known problems

Log in to edit this page

About this page

Drupal version
Drupal 6.x, Drupal 7.x
Audience
Designers/themers, Programmers

Theming Guide

Drupal’s online documentation is © 2000-2013 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License. Comments on documentation pages are used to improve content and then deleted.
nobody click here