Voting starts in March for the Drupal Association Board election.
Our recent theme improvements bring us a lot closer to a clean separation of content and presentation. But rendering is still hard-coded to XHTML templates. Themed data - e.g., of page requests - are accessible only in their fully rendered output. The structured data are prepared for rendering, but we don't have a way to specify e.g. that a particular page request should be rendered in a different format.
A specific problem is how to fetch a page in JSON rather than rendered HTML format, to enable selective updating of specific page elements as opposed to full page refreshes.
The attached patch enables AJAX/AHAH requests through our regular page request handlers. The approach:
1. Enable override of the theme function in
theme() to specify a different renderer.
Just before rendering, we call a new function,
drupal_set_renderer(). This function calls
hook_renderer(), which enables modules to specify a renderer. The theme hook and variables are passed, the variables by reference, so that any preparation of variables for output can be done.
2. Provide a single
system_renderer(). Here we handle jQuery requests for pages. If a page is being requested through a jQuery request and has reached the
theme('page') stage, we can assume it's not the full page content that's wanted. Instead, we redirect rendering to JSON, through a new function,
drupal_render_js(). Before sending to JS rendering, we filter the page variables so as to limit the size of the return value as well as preventing exposure of unfiltered data.
3. Move calls from index.php into a new function,
drupal_page_content(), to make them reusable. For example, a module might wish to fetch a page render after resetting the value of
drupal_site_offline() return content rather than directly rendering it, so that their content can be returned in JSON format via regular page requests.
5. Add a new argument, 'menu_result', to
theme('page') calls. This value will be
TRUE for regular page renders but for not found, access denied, and site offline renders will return the appropriate menu constant. This is useful e.g. in AJAX responses.